try/catch/finally Summary Attempts to execute a block of statements and specifies a response if an exception is thrown. Syntax try { try_statements; } catch([type1] exception_name_1) { catch_statements_1; } [catch([type2] exception_name_2) { catch_statements_2; }] [...] [catch([typeN] exception_name_N) { catch_statements_3; }] [catch(...) { catch_statements_N; }] [finally { finally_statements; }] Parameters try_statements The statements to attempt to execute. typeN The type of the exception to catch. exception_name_N The identifier to associate with the exception object for the respective catch clause. catch_statements_N Statements to execute if a matching exception was thrown. finally_statements Statements to execute after the try block completes regardless of whether an exception was thrown or caught. Description The try/catch/finally statements allow you to execute code which might throw exceptions. If an exception is caught, control passes to the relevant catch clause. The finally clause will always execute after all the try and catch statements have executed. For example, a contrived HTTP GET request function named "getRemoteFile" might throw exceptions if the URL was not found (URLNotFoundException). In this case, we do not want our program to terminate, but we might want to display a custom error message to the user instead. The code might look like: 123456try { getRemoteFile("http://www.fake-url.com/missing-file.html");}catch(URLNotFoundException ex) { showError("File was not found! Please choose another file.");} In another case, "getRemoteFile" might throw another exception of type "ServerTimeoutException" if the request timed out. If we want to catch both exceptions, our code might look like: 123456789try { getRemoteFile("http://www.fake-url.com/missing-file.html");}catch(URLNotFoundException ex) { showError("File was not found! Please choose another file.");}catch(ServerTimeoutException ex) { showError("Request to the server timed out. Please try again later.");} External Exceptions If you're executing JavaScript code that might potentially throw an exception, catching the external exception requires the same code as in JavaScript. 123456789import System; try { var foo = null; foo();}catch(e) { Console.error(e);} If the type is not specified for the exception, it defaults to external as shown above. Catching All Exceptions Sometimes, we might not want to catch every single type of exception. In this case, we can omit the exception type and catch all non-external exceptions with System.Exception: 12345678import System; try { getRemoteFile("http://www.fake-url.com/missing-file.html");}catch(System.Exception ex) { showError("An error occurred!");} The "catch all" above will only catch non-external exceptions that were not already caught. For example, we can catch some exceptions and then leave the rest to the "catch all": 1234567891011121314try { getRemoteFile("http://www.fake-url.com/missing-file.html");}catch(URLNotFoundException ex) { showError("File was not found! Please choose another file.");}catch(ServerTimeoutException ex) { showError("Request to the server timed out. Please try again later.");}catch(System.Exception ex) { // This only executes if the exception was not of types // "URLNotFoundException" and "ServerTimeoutException" showError("An error occurred!");} To explicitly catch all external and internal exceptions, catch both the external type and the System.Exception type: 1234567891011import System; try { doSomething();}catch(System.Exception ex) { Console.error("Internal exception: " + ex.getMessage());}catch(ex) { Console.error("External exception: " + e);} If you don't need the exception object, JS++ provides a convenience syntax to catch all exceptions (both internal and external exceptions) in just one catch clause using the ... syntax: 12345678import System; try { doSomething();}catch(...) { Console.error("Exception caught.");} If a ... clause is used, there is no exception object that can be accessed, but it will completely catch all potential exceptions with just one catch clause. Clause Expectations The try statement expects at least one catch clause, at most one finally clause, or both. Therefore, there are three possible combination of try/catch/finally statements: try...catch try...finally try...catch...finally If no exceptions were thrown, none of the catch clauses will be executed. On the other hand, the finally clause will always execute regardless of whether an exception was thrown or caught. The finally clause always executes after the try statements and catch statements. Differences to JavaScript Unlike JavaScript, JS++ try statements support multiple catch clauses and typed catch clauses. Examples Basic Usage 1234567try { // attempt to call myFunction myFunction();}catch(e) { // if an error occurred, do something here} Multiple Catch Clauses for JS++ Code 123456789try { jsppFunction();}catch(System.Exceptions.CastException ex) { // handle CastException}catch(System.Exception e) { // catch-all to catch any other internal exceptions} Catch all internal and external exceptions 123456789try { myFunction();}catch(System.Exception e) { // handle all internal exceptions}catch(external e) { // handle all external exceptions} Catch-all Clause (catches all internal and external exceptions) 12345try { myFunction();}catch(...) {} 'finally' Clause 1234567string myFile = openFile();try { writeToFile(myFile, someData);}finally { closeFile(myFile);} See Also System.Exception System.Exceptions throw Share HTML | BBCode | Direct Link