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: 

public class Main { public static void main(String[] args) {int even_Numbers []={2,4,6,8};System.out.println(even_Numbers[7]);}

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: 

public class Main { public static void main(String[] args) { try { int even_Numbers[] = {2, 4, 6, 8};System.out.println(even_Numbers[7]); } catch (Exception e){ System.out.println("The Array does not have an index number upto 7"); }} }

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: 

public class Main { public static void main(String[] args) { try { String programming_language[] = {"Python","Java","C programming","C# Programming"};for(int ample=0;ample<=10;ample++){ System.out.println(programming_language[ample]); }} catch (Exception e){ System.out.println("The loop can not continue till 10 as the array (programming_languages) contains only 4 values"); }} }

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: 

public class Main { public static void main(String[] args) { try { float student_heights[] = {5.1f,6.2f,5.3f,6.8f,6.1f};for(int mrx=0;mrx<=10;mrx++){ System.out.println(student_heights[mrx]); } } catch (Exception e){ System.out.println("The loop can not continue till 10 as the array (programming_languages) contains only 4 values"); } finally { System.out.println("The Code until the error was shown will be shown by the output after going through the try and catch block"); } } }

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: 

public class Main { public static void main(String[] args) { try { char characters[] = {'A','B','C','D','E'};System.out.println(characters[8]); } catch (Exception e){ System.out.println("There is no value at index 8 of the above array"); } finally { System.out.println("After passing through the try and catch block the following block will execute the rest of the code"); } } }

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: 

public class Main { static void check_Height(double height) { if (height <= 4.10) { throw new ArithmeticException("You are a DWARF "); } else { System.out.println("You have a tall height"); } }public static void main(String[] args) { check_Height(4.07d); } }

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: 

public class Main { static void check_Height(double height) { if (height <= 4.10) { throw new ArithmeticException("You are a DWARF "); } else { System.out.println("You have a tall height"); } }public static void main(String[] args) { check_Height(4.17d); } }

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: 

public class Main { static void check_Even(int odd_number) { if (odd_number%2==0) { throw new ArithmeticException(odd_number+" is an Even number"); } else { System.out.println(odd_number+" is an Odd number"); } }public static void main(String[] args) { check_Even(6); } }

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: 

public class Main { static void check_Even(int odd_number) { if (odd_number%2==0) { throw new ArithmeticException(odd_number+" is an Even number"); } else { System.out.println(odd_number+" is an Odd number"); } }public static void main(String[] args) { check_Even(17); } }

 

We value your feedback.
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0

Subscribe To Our Newsletter
Enter your email to receive a weekly round-up of our best posts. Learn more!
icon

Leave a Reply

Your email address will not be published. Required fields are marked *