}catch(exceptiontype1 e_1) {
//Catch block 1
}
catch(exceptiontype2 e_2) {
//Catch block 2
}catch(exceptiontype3 e_3) {
//Catch block 3
}
This code uses three catches. However, you can use as many catch blocks as you need for
your code. On the off chance that an exception happens
in the protected code, the
exemption is thrown and caught firstly by the first catch block. If the exception type
matches, then the catch block executes. However, if the exception type doesn’t match, the
exception is open to be caught by the next catch block. This process
continues until a
matching exception type is found or all the catch blocks have been checked.
Sample Implementation:
try{
filex = new Fileinputstream(nameoffile);
num = (byte) filex.read();
}catch(IOException e_1) {
e_1.printstacktrace();
return -1;
}catch(filenotfoundexception f_1){
f_1.printstacktrace();
return -1;
}
Throws Keyword
On the off chance that a system does not handle a checked exception,
the method must
proclaim it utilizing the keyword throws. The throws keyword shows up toward the end of
a the method’s signature.
You can throw an exemption, either a recently instantiated one or
a special
case that you simply found, by utilizing the keyword throw.
Finally Keyword
The keyword finally is utilized to make a piece of code that last code to be executed for a
program. A finally square of
code dependably executes, irrespective of whether an
exemption has happened or not. Utilizing a finally piece permits
you to run any cleanup-
sort statements that you need to execute, regardless of what happens in the secured code.
Do'stlaringiz bilan baham: