Advertisement

Try/Catch Support

Started by December 03, 2018 04:42 PM
1 comment, last by WitchLord 5 years, 11 months ago

I noticed this in the 2.33.0 WIP page:

"Added support for try/catch statements"

I'm sure you will elaborate the feature in the documentation, but I was curious if this "try/catch" behavior was going to include "throw" statements?  If so, would the "catch" statement be able to catch a specific kind of exception?  Could there be some sort of variable or function accessible from the "catch" statement that could contain information about the exception?

Thank you

I've opted for a quite simple solution. The compiler/VM only has a simple try/catch statement to capture any exception and unwind the stack. 

The application can then register its own methods for throwing exceptions explicitly and for inspecting caught exceptions. The standard add-on of course come with a couple of pre-implemented solutions that the applications can use if you prefer not to implement your own.

Here's the excerpt from the doxygen source for the WIP version:




\section try Try-catch blocks

<pre>
 {
   try
   {
     DoSomethingThatMightThrowException();
  
     // This is not executed if an exception was thrown
   }
   catch
   {
     // This is executed if an exception was thrown
   }
  }
</pre>

A try-catch block can be used if you're executing some code that might throw an exception 
and you want to catch that exception and continue with the exception rather than just abort 
the script.

\subsection try_func Functions

\note The standard <tt>throw</tt> and <tt>getExceptionInfo</tt> are only 
provided if the application \ref doc_addon_helpers_try "registers them". 

<b>void throw(const string &in exception)</b>

Explicitly throw an exception. The string should identify the type of exception, for logging or treating.

<b>string getExceptionInfo()</b>

Get the exception string for the last exception thrown.

 

One enhancement over the standard add-on that I can think of is to allow the throw and getExceptionInfo carry a dictionary with further information on the exception, e.g. location, callstack, and perhaps user defined parameters.

 

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

This topic is closed to new replies.

Advertisement