Java: Java Programming For Beginners a simple Start To Java Programming (Written By a software Engineer)


Download 0.82 Mb.
Pdf ko'rish
bet67/95
Sana09.01.2022
Hajmi0.82 Mb.
#261112
1   ...   63   64   65   66   67   68   69   70   ...   95
Bog'liq
Java Programming For Beginners - A Simple Start to Java Programming ( PDFDrive )

Using Multiple Try Blocks
A  single  piece  of  code  can  have  a  number  of  catch  blocks  for  catching  different
exceptions. The structure of the multiple try/catch blocks is given below:
try {
//Statements to be tested


}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.

Download 0.82 Mb.

Do'stlaringiz bilan baham:
1   ...   63   64   65   66   67   68   69   70   ...   95




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling