Try-Catch Block
The
try statement allows you to define a block of code to be tested for errors.
The
catch statement allows you to define a block of code to be executed, if an error occurs in the try block.
try {
int[] myNumbers = {1, 2, 3};
System.out.println(myNumbers[10]); // Error
} catch (Exception e) {
System.out.println("Something went wrong.");
}
Finally Block
The
finally statement lets you execute code, after try...catch, regardless of the result.
try {
// code
} catch (Exception e) {
// handle error
} finally {
System.out.println("The 'try catch' is finished.");
}