Try-Catch Exceptions In Java
Java Try Catch is explained here in order to better meet the educational needs of learners.
Java Try-Catch Exceptions
There are different kinds of errors that can occur when executing Java code.
These errors include coding errors that the programmer makes, errors caused by incorrect input, or other unpredicted events.
The Java program usually crashes when an error occurs and generates an error message. It is technical to say that Java will throw an exception (throw an error) when it comes to Java Try Catch.
Java Try & Catch
While Java Try Catch is running, the try statement allows you to test a block of code for errors.
If an error occurs in the try statement, the catch statement still allows you to execute a block of code.
You’ll find the try and catch keywords in pairs:
Syntax
try { // The code in this block is executed } catch(Exception e) { // This block is used to control the errors of the code }
Here is an example:
This will result in an error, since Java Try Catch does not have an even_Number[7].
Example: 
As a result, you’ll see something like this:
Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: Index 7 out of bounds for length 4
at Main.main(Main.java:7)
Try-catch can be used to catch errors and execute some code to deal with them:
Example: 
Output:
The Array does not have an index number upto 7
Similarly, an array can be traversed upto the number of elements it contains but not more than that:
Example: 
With Java Try Catch, you can execute code after executing through the try and catch condition, regardless of the outcome, with the finally statement:
Example: 
The result will be as follow:
5.1
6.2
5.3
6.8
6.1
The loop can not continue till 10 as the array (programming_languages) contains only 4 values.
The Code until the error was shown will be shown by the output after going through the try and catch block:
Example: 
Following will be the result:
There is no value at index 8 of the above array
After passing through the try and catch block the following block will execute the rest of the code.
The throw keyword
The throw statement allows you to create a custom error for Java Try Catch.
Throw statements are implemented along with exception types. Exceptions in Java include ArithmeticException, FileNotFoundException, ArrayIndexOutOfBoundsException, and SecurityException and etc:
It will throw an exception if the height is below 4.10 feet as (“You are a dwarf”). When the height is 4.11 or higher, it will print “You have a tall height”:
Example: 
Results, will be as follow:
Exception in thread “main” java.lang.ArithmeticException: You are a DWARF
at Main.check_Height(Main.java:4)
at Main.main(Main.java:12)
You would not get an exception if the input height is greater than or equal to 4.11:
Example: 
Result will be:
You have a tall height
Correspondingly, an odd_number method can be used to determine if the input integer is odd or not. If the given integer is even it throws an exception as shown below:
Example: 
Since, we gave the input as 6 hence, it will throw an exception:
Exception in thread “main” java.lang.ArithmeticException: 6 is an Even number
at Main.check_Even(Main.java:4)
at Main.main(Main.java:12)
Similarly, if we put the integer value as 17:
Example: