Java try-catch blockJava try blockJava try block is used to enclose the code that might throw an exception. It must be used within the method. If an exception occurs at the particular statement of try block, the rest of the block code will not execute. So, it is recommended not to keeping the code in try block that will not throw an exception. Java try block must be followed by either catch or finally block. Syntax of Java try-catchSyntax of try-finally blockJava catch blockJava catch block is used to handle the Exception by declaring the type of exception within the parameter. The declared exception must be the parent class exception ( i.e., Exception) or the generated exception type. However, the good approach is to declare the generated type of exception. The catch block must be used after the try block only. You can use multiple catch block with a single try block. Problem without exception handlingLet's try to understand the problem if we don't use a try-catch block. Example 1Output: Exception in thread "main" java.lang.ArithmeticException: / by zero As displayed in the above example, the rest of the code is not executed (in such case, the rest of the code statement is not printed). There can be 100 lines of code after exception. So all the code after exception will not be executed. Solution by exception handlingLet's see the solution of the above problem by a java try-catch block. Example 2Output: java.lang.ArithmeticException: / by zero rest of the code Now, as displayed in the above example, the rest of the code is executed, i.e., the rest of the code statement is printed. Example 3In this example, we also kept the code in a try block that will not throw an exception. Output: java.lang.ArithmeticException: / by zero Here, we can see that if an exception occurs in the try block, the rest of the block code will not execute. Example 4Here, we handle the exception using the parent class exception. Output: java.lang.ArithmeticException: / by zero rest of the code Example 5Let's see an example to print a custom message on exception. Output: Can't divided by zero Example 6Let's see an example to resolve the exception in a catch block. Output: 25 Example 7In this example, along with try block, we also enclose exception code in a catch block. Output: Exception in thread "main" java.lang.ArithmeticException: / by zero Here, we can see that the catch block didn't contain the exception code. So, enclose exception code within a try block and use catch block only to handle the exceptions. Example 8In this example, we handle the generated exception (Arithmetic Exception) with a different type of exception class (ArrayIndexOutOfBoundsException). Output: Exception in thread "main" java.lang.ArithmeticException: / by zero Example 9Let's see an example to handle another unchecked exception. Output: java.lang.ArrayIndexOutOfBoundsException: 10 rest of the code Example 10Let's see an example to handle checked exception. Output: File saved successfully Internal working of java try-catch blockThe JVM firstly checks whether the exception is handled or not. If exception is not handled, JVM provides a default exception handler that performs the following tasks:
But if exception is handled by the application programmer, normal flow of the application is maintained i.e. rest of the code is executed. Next TopicMultiple catch block in java
|