What is the importance of try and catch block?
The 5 block in Java is used to handle exceptions and prevents the abnormal termination of the program. Show Here's the syntax of a 5 block in Java.
The 7 block includes the code that might generate an exception.The 8 block includes the code that is executed when there occurs an exception inside the 7 block.Example: Java try...catch block
Output ArithmeticException => / by zero In the above example, notice the line,
Here, we are trying to divide a number by zero. In this case, an exception occurs. Hence, we have enclosed this code inside the 7 block.When the program encounters this code, ArithmeticException => / by zero1 occurs. And, the exception is caught by the 8 block and executes the code inside the 8 block.The 8 block is only executed if there exists an exception inside the 7 block.Note: In Java, we can use a 7 block without a 8 block. However, we cannot use a 8 block without a 7 block.Java try...finally blockWe can also use the 7 block along with a finally block.In this case, the finally block is always executed whether there is an exception inside the try block or not. Example: Java try...finally block
Output Finally block is always executed Exception in thread "main" java.lang.ArithmeticException: / by zero at Main.main(Main.java:4) In the above example, we have used the 7 block along with the 2 block. We can see that the code inside the 7 block is causing an exception.However, the code inside the 2 block is executed irrespective of the exception.In Java, we can also use the finally block after the 5 block. For example,
Output Entering try statement Exception => Index 5 out of bounds for length 5 Closing PrintWriter In the above example, we have created an array named list and a file named output.txt. Here, we are trying to read data from the array and storing to the file. Notice the code,
Here, the size of the array is 6 and the last element of the array is at 7. However, we are trying to access elements at 8 and 9.Hence, the code generates an exception that is caught by the catch block. Since the 2 block is always executed, we have included code to close the 1 inside the finally block.It is a good practice to use finally block to include important cleanup code like closing a file or connection. Note: There are some cases when a 2 block does not execute:
For each 7 block, there can be zero or more 8 blocks. Multiple 8 blocks allow us to handle each exception differently.The argument type of each 8 block indicates the type of exception that can be handled by it. For example,
Output 0In this example, we have created an integer array named 9 of size 10.Since the array index starts from 0, the last element of the array is at Finally block is always executed Exception in thread "main" java.lang.ArithmeticException: / by zero at Main.main(Main.java:4)0. Notice the statement, 1Here, we are trying to assign a value to the index 10. Hence, Finally block is always executed Exception in thread "main" java.lang.ArithmeticException: / by zero at Main.main(Main.java:4)1 occurs. When an exception occurs in the 7 block,
Catching Multiple ExceptionsFrom Java SE 7 and later, we can now catch more than one type of exception with one 8 block.This reduces code duplication and increases code simplicity and efficiency. Each exception type that can be handled by the 8 block is separated using a vertical bar 1.Its syntax is: 2To learn more, visit Java catching multiple exceptions. Java try-with-resources statementThe try-with-resources statement is also referred to as automatic resource management. This statement automatically closes all the resources at the end of the statement. What is the purpose of try and catch block?The try statement allows you to define a block of code to be tested for errors while it is being executed. The catch statement allows you to define a block of code to be executed, if an error occurs in the try block.
Is try catch block necessary?Core Java bootcamp program with Hands on practice
Yes, It is possible to have a try block without a catch block by using a final block. As we know, a final block will always execute even there is an exception occurred in a try block, except System. exit() it will execute always.
|