Earlier in this learning journey, we looked at error handling using the TRY, CATCH, and ENDTRY statements. After the TRY statement comes the statement that might cause an error. If the error does occur, the system jumps to the CATCH statement and processes its contents. Whatever happens, the method carries on processing after the ENDTRY statement.
Sequence of CATCH Statements
When you write CATCH statements, you must place the most specific class (or classes) first and the most generic ones last. If you place a superclass above one of its subclasses, you will cause a syntax error. This is because the system could not process the specific CATCH block because it is overshadowed by the more generic one.
The INTO Addition
Each exception is represented by an object. You can place this object in a reference variable and work with it - for example, to retrieve the error text from the exception class using the get_text( ) method as shown in the example here.
You can use an inline declaration in the CATCH statement to ensure that the reference variable is declared with the correct type.
The most common thing to do with the exception object is to call its get_text( ) method. However, exception objects can possess other attributes and methods to help you handle the error.
The Attribute "Previous"
Sometimes within a call chain a method will catch an exception. In response, it will raise a different exception for its own caller to catch. This might happen, for example, if a framework raises an exception that is too technically-oriented for the application to process by itself. However, in certain circumstances the method that does this (method 2 in the example) may want to trigger its own exception, but also pass on the first exception. It can do this using the previous attribute of the exception class.
When you raise an exception, you can pass an instance of an exception class to the importing parameter previous. This attaches the exception object to the new exception. A method that catches the second exception can access the first exception object using the public read-only attributes previous.
Previous is implemented in the superclass CX_ROOT, and is therefore available in all exception classes.