Exception handling in ASIL is largely modeled after Java's exception handling. Exceptions must derive from one of the following:
Exception base class/interface | What it does |
---|---|
IThrowable | All exception classes must implement this interface |
Exception | If a property or procedure throws anything of type Exception or any class derived from Exception, it must be declared with the throws clause or handled with a try/catch block. |
Error | This is an abstract class whose derived types don't need to be caught. They are meant for dynamic errors. |
Even though exceptions of type Error don't need to be caught, all compilers should provide an option to require instances of an Error type and its derivatives to be caught. This could be useful when the author of a exception class use Error when they should have picked Exception as their base type.
The throws keyword can be applied to any procedure as shown below.
command DoThis throws Exception ' Run some code that might throw Exception
If a property needs to throw an exception, the throws clause should be applied to the accessor, not the property.
property String MyProp get throws Exception ' Run some code that might throw Exception set ' This accessor doesn't throw any exceptions
If you need to catch rather than declare an exception, use this syntax:
try ' code that might throw an exception catch Exception e ' handle the exception
This sample show how to handle multiple exception types:
try ' code that might throw several different types of exceptions catch ExceptionA e ' Handle this exception catch ExceptionB e ' Handle this one catch Exception e ' Handle all other exceptions
You can use a finally block to ensure a variable is initialized or to allow the garbage collector to deallocate a variable.
try ' Code the might throw an exception catch Exception e ' Handle that exception finally ' clean up
If you're declaring exceptions rather than catching them, you still might want a try block so you can have a finally block clean up.
command CallMe throws Exception try ' Do something that might throw an exception finally ' clean up, but don't handle the exception
If you have a try block, you must either follow it with at least one catch block or a finally block. All compilers must catch this situation.
Wiki: Home
Wiki: keywords-catch
Wiki: keywords-finally
Wiki: keywords-throw
Wiki: keywords-throws
Wiki: keywords-try