Work of try-catch-finally block in Java
try - this keyword is used to mark the beginning of a block of code that could potentially lead to an error.
catch is a keyword to mark the beginning of a block of code to catch and handle exceptions if they occur.
finally is a keyword to mark the start of a block of code that is optional. This block is placed after the last catch block. Control is passed to the finally block in any case, whether an exception was thrown or not.
The general view of the construction for handling an exception is as follows:
try {
// code that can potentially lead to an exception
} catch(SomeException e) { // the class of the specific expected error is indicated in parentheses
// exception handling code
} finally {
// optional block, the code of which is executed anyway
}
Mechanism try-with-resources
This construct, which appeared in Java 7, allows you to use a try-catch block without worrying about closing resources used in a given code segment. Resources are declared in parentheses immediately after try, and the compiler itself implicitly creates a finally section, in which the resources occupied in the block are released. Resources are defined as entities that implement the java.lang.Autocloseable interface.
General view of the structure:
try(/*resource declaration*/) {
//...
} catch(Exception ex) {
//...
} finally {
//...
}
It is worth noting that catch and explicit finally blocks are executed after resources are closed in an implicit finally.
Is it possible to use a try-finally block (no catch)?
Such notation is acceptable, but there is not much sense in such notation, it is still better to have a catch block in which the necessary exception will be handled.
Can a single catch block catch multiple exceptions at once?
In Java 7, a new language construct has become available, with which you can catch multiple exceptions with one catch block:
try {
//...
} catch(IOException | SQLException ex) {
//...
}
Are there situations where the finally block will fail?
For example, when the JVM "dies" - in such a situation finally is unreachable and will not be executed, since a forced system exit from the program occurs:
try {
System.exit(0);
} catch(Exception e) {
e.printStackTrace();
} finally { }
Can the main() method throw an exception outside, and if so, where will this exception be handled?
It can and exception will be transferred to the Java virtual machine (JVM).
Suppose there is a method that can throw an IOException and FileNotFoundException in what order should the catch blocks go? How many catch blocks will be executed?
As a general rule, you need to handle exceptions from "lowest" to highest. You cannot put in the first catch(Exception ex) {} block, otherwise all further catch() blocks will not be able to process anything, because any exception will match the catch(Exception ex) handler.
Thus, based on the fact that FileNotFoundException extends IOException, the FileNotFoundException must be handled first, and then the IOException:
void method() {
try {
//...
} catch (FileNotFoundException ex) {
//...
} catch (IOException ex) {
//...
}
}
Read also:
Comments
Post a Comment