The block of code that has to be tested for exception is surrounded by try statement. The error caught during try block execution than gets handled within the catch statement. When and error occurs, the try block is stopped and the catch” block is executed. Errors may be customized within throw statements. Following an error being "caught" or not, a finally statement may be added to execution that will be executed regardless if an error has occured or not.
Syntax for try-catch-finally statements
// code containing error
//exception cause for case 1
//exception cause for case 2
//general case of exception
//code to be executed compulsorily.
There can be multiple exceptions or errors which could be caught. In such cases, there can be if-else statements inside the catch to throw separate errors which in turn will help in debugging.
There are 3 variations of try/catch blocks:
Example of try-catch-finally statements