Try-Catch-Finally (Reserved Word)
The try, catch, and finally reserved words work together to make up a code structure that is used to test for exceptions and handle them when they occur. Generally, a Try-Catch-Finally statement consists of a Try block is followed by one or more Catch clauses to handle exceptions and, optionally, a Finally block to clean up resources before exiting the statement. The guarded code in the Try block is executed until an exception is thrown or until the all of the try block code is successfully executed. If an exception is thrown, the Catch block is examined for a statement that handles the exception. If no catch statement is found, the system generates an error message and program execution may halt. If a Finally block is present, it will be the last set of code executed in the statement regardless of whether any exceptions have been thrown.
Remarks
Try-Catch-Finally blocks are used to handle anticipated exceptions in your code so that you may deal with the exception and/or display a specific error message to the user.
- Try is used to test for an occurrence of an anomalous situation (exception) during the program execution.
- Catch is used to execute additional code in response to specified exception cases.
- Finally is used to execute code after any try and catch blocks complete. It is useful for cleaning up any resources allocated in the try block as well as running any code that must execute even if there is an exception. Control is always passed to the finally block regardless of how the try block exits.
The following describes the order of processing in a try-catch-finally block.
- Control reaches the try statement by normal sequential execution. The guarded section within the try block is executed.
- If no exception is thrown during execution of the guarded section, the catch clauses that follow the try block are not executed. Execution continues at the statement after the last catch clause following the associated try block.
- If an exception is thrown during execution of the guarded section or in any code the guarded section calls (either directly or indirectly), the catch handlers are examined in order of their appearance following the try block.
- If a matching catch handler is found, the catch handler is executed and the program resumes execution following the last handler (that is, after the End statement of the try-catch block). Control can only enter a catch handler through a thrown exception.
- If a matching catch handler is not found, program execution resumes after the last handler.
- If a finally block is present, the code within the block is executed just before the try-catch-block is exited. This is used to guarantee that a block of statements executes regardless of how the preceding try block is exited.
Example 1
In this example, an xmldocument file object is created when the analysis technique is initialized and an attempt is made to load the file specified in the try block. If the file is not found, a filenotfoundexception is thrown and is caught by the matching catch clause that includes a throw statement to generate a user defined message that appears in the Event Summary message section of the TradeStation Events Log.
You can also use catch (exception
ex) to handle any exception which can be useful when troubleshooting.
var: elsystem.xml.xmldocument doc(nuLL);
method void AnalysisTechnique_Initialized( elsystem.Object sender, elsystem.InitializedEventArgs args )
begin
doc = new
elsystem.xml.xmldocument;
try
doc.Load("C:\testing123.xml");
catch (elsystem.io.filenotfoundexception
ex)
throw ex.create("User-defined: File is not in the specified
directory");
end;
end
Example 2
The following psuedo-code shows the structure of a try-catch-finally block with a finally clause:
try
// statements to be tested for an
exception
catch (elsystem.exception
ex)
// statements that handle a
general exception
finally
// statements that always executed
after any try or catch statements are executed
end;