Note that the finally -block executes regardless of whether an exception is thrown. Also, if an exception is thrown, the statements in the finally -block execute even if no catch -block handles the exception.
The following example shows one use case for the finally -block. The code opens a file and then executes statements that use the file; the finally -block makes sure the file always closes after it is used even if an exception was thrown. Now, if we already caught the exception in the inner try -block by adding a catch -block. Any given exception will be caught only once by the nearest enclosing catch -block unless it is rethrown. Of course, any new exceptions raised in the "inner" block because the code in catch -block may do something that throws , will be caught by the "outer" block.
If the finally -block returns a value, this value becomes the return value of the entire try-catch-finally statement, regardless of any return statements in the try and catch -blocks. This includes exceptions thrown inside of the catch -block:. The outer "oops" is not thrown because of the return in the finally -block. View all Courses. All rights reserved. Courses Pricing. How to use try-catch statements in Java.
Try: The block of code to be tested for errors while the program is being executed is written in the try block. Catch: The block of code that is executed when an error occurs in the try block is written in the catch block.
Why use try-catch statements? In the code below, we have an arithmetic error : division by zero. The 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 the application programmer handles the exception, the normal flow of the application is maintained, i. 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 might be lines of code after the exception. If the exception is not handled, all the code below the exception won't be executed. As displayed in the above example, the rest of the code is executed, i. Here, we can see that if an exception occurs in the try block, the rest of the block code will not execute. 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.
JavaTpoint offers too many high quality services. Mail us on [email protected] , to get more information about given services. Please mail your requirement at [email protected] Duration: 1 week to 2 week.
Exception in thread "main" java. FileNotFoundException; import java. Next Topic Multiple catch block in java. Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support. Place any code statements that might raise or throw an exception in a try block, and place statements used to handle the exception or exceptions in one or more catch blocks below the try block.
Each catch block includes the exception type and can contain additional statements needed to handle that exception type. In the following example, a StreamReader opens a file called data. Since the code might throw any of three exceptions, it's placed in a try block.
0コメント