! '0''g0#0 "78/4'9"+ "%8 :;"1'(<""='4#,11'(C/61'(.,51'(1,+C2/WA5-#'59">'?'.'@' <""='#'& %B 1%$B @C2/61,+1'(.,51'(1,+C2/WA5-#'5 %B1='4"5G@29"NO/LA+'$='&,6B %B 9I29"1'(.,51'(1,+C2/WA5-#'57/='&,B6 try, catch <# finally /LA+'$11'( .,51'(1,+C2/WA5-#'5 <#D/" 2'$C/6+ .:;"1'(<""='1'(&(2'6#'&:(> C2/WA5-#'5CS8"0'940H
! g0#0 !:(<1(0>'?'.'@'/'.1A5C2/WA5-#'5}bXCS8"9"C,8"D/"1'((,"!:(<1(0 !5$ C%B 2/WA5-#'54#H'"%8.G0H&'0'(*D(@.&/+G529"C,8"D/"1'(/0G-#E!:(<1(0 D,@/$H'6 IH"='&,6B offset = x/n; &='4(,+D,@<:( 0%B %I"A5C2/0M#:;" int %BI7B/ offset, x <# n .:;"='&,B6 %B &'0'(*/0G-#EWH'"G52
'?'.'@'.D(@.&/+-+C2/WA5-#'59"1'(4'(52@$ .='"@"DK0)M"$E<#.&H6C2/WA5-#'5 bX %B1A5CS8"1#,+0' !:(<1(0>'?'.'@'<+H6C2/WA5-#'5 %B/'.1A5CS8"C(,"!:(<1(0:;"&/6:(> 7/ } :;"C2/WA5-#'5 %BG0H&'0'(*<12GC<#.,51'(G52 IH" Virtual MemoryError <# OutOfMemoryError :;"D2" ('.G0H1#H'@*S6 } "7B/6.'1} :;"C2/WA5-#'5 %B('G0H&'0'(*C%$"!:(<1(0-7B/<12GC <#.,51'(G52 }bX :;"C2/WA5-#'5 %B&'0'(*<12GC<#.,51'(G52 IH" C2/WA5-#'5 .'11'(:5GJ#E %BG0H0%/$MH9"G5(K1 /(%B (FileNotFoundException) 4(7/C2/WA5-#'5.'11'(/2'6/A640'$#CC/6&0'IA1C/6/($E %BG0H*M1D2/6 7/ G0HG520%/$MH.(A6 (ArrayIndexOutOfBoundsException)
C2/WA5-#'59">'?'.'@'.1='4"5:;"//:.KC/6#'&DH'6F0'11@H'41&A+#'& !5$0%#='5,+1'(&7+ /55,6<&569"(M: %B sQP RSB6#'&C/6C2/WA5-#'54#H'"%.8 &7+ /50'.'1 #'&ThrowableRSB6:;"#'&('1
5! #'&C/6C2/WA5-#'5DH'6F &%B 7+ /50'.'1#'& I%B /7B Throwable
! MkGJSCANI #'& %BI7B/ Exception :;"#'& %B1='4"59" NO -7B/(+3C2/WA5-#'5 %B 1A5CS8"9"C(,"!:(<1(0>'?'.'@' }bX<+H6//1:;"&/6:(> 7/ PQ RuntimeException :;"C2/WA5-#'5 %B/'.4#%1#%$B 6G524'10%1'(C%$" !:(<1(0 %B*M1D2/6 D,@/$H'6IH" ArrayIndexOutOfBoundsException RSB6:;"C2/WA5-#'5-('0%1'(/2'6/A6&0'IA1C/6/($E %BG0H*M1D2/6 &'0'(* <12GC!:(<1(09425%CS8"G52*2'0%1'(D(@.&/+40'$#C&0'IA1C/6/($E1H/" %B. /2'6/A6!5$9I2='&,6B if
!"!#$%&'(&" )&*'+," !"!#$%-(./01#2'.2'3 4'(#'51(+,6
Q
IOException
:;"C2/WA5-#'5 %B/'..G0H&'0'(*<12GC!5$1'(:(,+:(36 !:(<1(0942&0+M(ECS8"G52 IH"UnknownHostException %B/'.1A5CS8"9" C %B!:(<1(01='#,6-$'$'0DA5DH/1,+(++/A"D/(E"KD "%8G0H &'0'(*D(@.&/+G52!5$9I2='&,6B if IOException .D2/60%1'(C%$"='&,6B try/catch 9"1'(.,51'(1,+C2/WA5-#'5 %B 1A5CS8" #'&:(> }bX %B&=',n<#-++H/$9"!:(<1(0>'?'.'@'0%5,6"%8 x ArithmeticException
:;"1'((+3@H'0%C2/WA5-#'59""A-."EAD)'&D(EIH" 1'(4'(52@$#C.='"@" DK0)M"$E x ArrayIndexOutOfBoundsException
:;"1'((+3@H'0%1'(/2'6/A6&0'IA1C/6/($EG0H*M1D2/6 :;"#C.='"@"DK0#+ 4(7/0%H'0'11@H'40'$#C&0'IA1C/6/($E %B0%/$MH x EOFException
:;"1'((+3@H'D='<4"H6&A8"&35C/6GJ#EG52WH'"0'<#2@ x FileNotFoundException
:;"1'((+3@H'G0H-+GJ#E %BD2/61'(/2'6/A6 x InterruptedException
:;"1'((+3@H' X+'6 X*M1(6,+ x IOException
:;"1'((+3C2/WA5-#'5 %B 1A5CS"8 .'1C+@"1'(/A"-3D4(7//' -E 3D95F x NullPointerException
:;"1'((+3@H'0%1'((%$19I20L/5C/6#'&.'1//:.K %B$,60%D='<4"H6/2'6/A6 :;"null/$MH//:.K$,6G0HG52*M1&(2'6 x NumberFormatException
:;"1'((+3@H'(M:<++C/6D,@#C %B9I2/$MHG0H*M1D2/6 !:(<1(0 %B sQP <&56D,@/$H'6!:(<1(0 %B 1A5C2/WA5-#'5 %BI7B/ ArrayIndex
("' 5! D,@/$H'6<&56C2/WA5-#'5 %BI7B/ArrayIndexOutOfBoundsException public class ExceptionDemo { public static void main(String args[]) { System.out.println(args[2]); System.out.println("Hello"); } }
5!$ W##,-LE %BG52.'11'((,"!:(<1(0 %BsQP
! A try..catch >'?'.'@'0%%$E @A(E5 try %B:;"='&,B6 9%B I29"1'(.,51'(1,+0L/54(7/='&,B6 %B/'. 1A5C2/WA5-#'5RSB6.&H6//:.K:(> }bX9"C(,"!:(<1(0%$E@A(E5 try.0% I35='&,6B /$MH>'$9"+#K/1!5$0%(:M <++5,6"%8
try { [statements] }
!:(<1(0>'?'.'@'.&,B66'"I35='&,B6 %B/$MH9"+#K/1 #% ='&,B6 <#4'11A5 C2/WA5-#'5CS8"9"='&,6B 951K.0%1'(&H6//:.KC/6C2/WA5-#'5:(> }bX ",8"CS8"0' RSB6!:(<1(0.$1#A11'( ='6'"='&,B6 %B/$MH9"+#K/1 %B 4#7/ ,86405 }bX %B D2/61'(.,51'(!5$0%(M:<++='&,B65,6"%8 catch(ExceptionType argumentName){ [statements] }
!"!#$%&'(&" )&*'+," !"!#$%-(./01#2'.2'3 4'(#'51(+,6
!5$ %B x
ExceptionType
7/I7B/#'&:(> }bX %BD2/61'(..,51'(07B/0%
C2/WA5-#'51A5CS8" x
argumentName
7/I7B/C/6//:.K %B.:;" %B9I29"+#K/1='&,6B
C/6catch !:(<1(0>'?'.'@'. ='I35='&,B69"+#K/1 catch*2'0%='&,B69"+#K/1 try='&,B6 95='&,6B 4"SB61A5C2/WA5-#'5!5$&H6//:.K:(> }bXD'0I"A5 &%B /5#2/61,+#'& %B(+39" ExceptionType <#!:(<1(0.C2'0='&,B6 %B 4#7//$MH9"+#K/1 try ,86405&H@"9"1(% G%B 0H0%='&,B6959"+#K/1 try1A5C2/WA5-#'5CS8"!:(<1(0>'?'.'@'. G0H0%1'( ='I35='&,B69"+#K/1catch!:(<1(0 %BsQ<&56D,@/$H'61'(.,51'(1,+C2/WA5-#'5 %B/'.1A5CS8"9"!:(<1(0 %B sQP!5$!:(<1(0"%8G52-AB0I35='&,6B try <# catchCS8"0'RSB6 !:(<1(0"%8.942W##,-LE5,6<&569"(M: %BsQc ("' 5!$D,@/$H'61'(.,51'(1,+C2/WA5-#'5 %B/'.1A5CS8" class ExceptionHandlingDemo { public public static void main(String args[]) { try { System.out.println(args[2]); } catch(ArrayIndexOutOfBoundsException ex) { System.out.println("There is no third command line argument"); } } }
5!%W##,-LE G%B 52.'11'((,"!:(<1(0 %BsQ
! '0''g0#0+C !:(<1(0>'?'.'@'&'0'(*.0%I35='&,B6C/6+#K/1 catch G520'11@H'4"SB6I35 &='4(,+9" }bX %B/$MH9"I35=" &,6B C 6+#K 1 catch .D2 6(%$6D'0#="5,+1" (&7+ 5 !:(<1(0 %B sQc <&56D,@ $H'61'( .,51'(1,+C2 WA5-#'50'11@H'4"SB6:(> !5$0%I35='&,6B 9"+#K/1 catch &/6I35-7/B .,51'(1,+C2/WA5-#'5&/6I"A57/ ArithmeticException<#ArrayIndexOutOf BoundsException !:(<1(0"%. 8 (,+ WH'" '6XVRSB6.:;"D,@ #C 0%B I% "A5C2/0M#:;" String<#[email protected].*M1<:#69420I% "A5C2/0M#:;"intRSB64'1C2/0M#:;" .='"@"DK0)M"$E1.K ='9421A5C2/WA5-#'5I"A5 ArithmeticException CS8"G52 !5$ !:(<1(0.C2'00' ='I35='&,B69"+#K/1 catch %BD(@..,+//:.KI"A55,61#H'@ <# !:(<1(0..,51'(1,+C2/WA5-#'5I"A5 NumberFormatException *2' %B(,+ WH'" '6 XV G0H9IHC2/@'0 %B<:#6:;"D,@#C.='"@"DK0G52 !5$!:(<1(0.C2'0 0' ='I35='&,6B 9"+#K/1 catch %BD(@..,+//:.KI"A5 NumberFormatException !5$0%W##,-LEC/6!:(<1(05,6D,@/$H'6 <%B &569"(M: %BsQu 9"1(% %B0C% 2/WA5-#'51A5CS8" >'?'.'@'.-A.'('@H':;"C2/WA5-#'5I"A595 RSB6 1'( %B..,51'(1,+//:.K:(> }bX ",8".-A.'('.'1#'& 0%B %1'(&7+ /5D'0 #='5,+I,8" ,86"%8 ('&'0'(*..,51'(1,+//:.K:(> }bX !5$9I2#'& %B :;" X C/6//:.K",8"G52 /' AIH"//:.KI"A5 FileNotFoundException &'0'(*.,51'(G52!5$9I2#'& IOException 4(7/ Exception < "G52"7B/6.'1#'& FileNotFoundException &7+ /50'.'1#'& IOException RSB6&7+ /50'.'1 #'&Exception/%1I,8"4"SB6 ("' !"D,@/$H'61'(.,51'(1,+C2/WA5-#'50'11@H'4"SB6:(> public class ExceptionHandlingDemoV2 { public static void main(String args[]) { try { int i = Integer.parseInt(args[0]); System.out.println(4 / i); } catch(ArithmeticException ex) { System.out.println(ex.toString()); } catch(NumberFormatException ex) { System.out.println("Invalid numeric format"); } } }
!"!#$%&'(&" )&*'+," !"!#$%-(./01#2'.2'3 4'(#'51(+,6
5!,W##,-LE G%B 52.'11'((,"!:(<1(0 %B sQc >'?'.'@'1='4"5942I35='&,B69"+#K/1 catch .D2/6(%$6//:.KC/6#'& :(> Exception D'0#='5,+1'(&7+ /5 D,@/$H'6IH" !:(<1(0 %B sQc *S6<02@H'.0% #'&:(> }bX %B.D(@..,+&/6I"A5
! #*'finally >'?'.'@'0%%$E @A(E5 finally %B.0%I35='&,B6/$MH9"+#K/1-7B/(+3942!:(<1(0 =' I35='&,6B 5,61#H'@4#,6.'1&A8"&351'( ='6'"C/6I35='&,B69"+#K/1 try 4(7/ catch!:(<1(0 %B sQt <&56D,@/$H'61'(1='4"5I35='&,B69"+#K/1 finally RSB6!:(<1(0"%.8 942W##,-LE5,6 <&569"(M: %BsQt >'?'.'@'. ='I35='&,B69"+#K/1 finally &0/ <02@H'.0%='&,B6 return 9" +#K/1 try4(7/ catch1H/"1KD'01(%5%$@ %B.G0H ='I35='&,B69"+#K/1 finally7/0% ='&,B6 System.exit(); -7B/ %B.//1.'1!:(<1(0 !:(<1(0 %B sQ
<&56D,@/$H'6 0%B % ='&,B6return /$MH1H/"I35='&,6B 9"+#K/1finally!5$!:(<1(0"%8.G52W##,-LE5,6(M: %BsQ
("' 5!- D,@/$H'61'(1='4"5I35='&,B69"+#K/1finally public class FinallyDemo { public static void main(String args[]) { try { System.out.println(args[2]); System.out.println("Hello"); } catch(ArrayIndexOutOfBoundsException ex) { System.out.println("There is no third argument"); } finally { System.out.println("Finish running the program"); } } }
!"!#$%&'(&" )&*'+," !"!#$%-(./01#2'.2'3 4'(#'51(+,6
("' 5!. D,@/$H'6 0%B %='&,B6return /$MH1H/"I35='&,6B 9"+#K/1finally public class FinallyDemoV2 { public static void main(String args[]) { FinallyDemoV2 obj = new FinallyDemoV2(); obj.myMethod(args); } public int myMethod(String args[]) { try { System.out.println(args[2]); return 0; } catch(ArrayIndexOutOfBoundsException ex) { System.out.println("There is no third argument"); } finally { System.out.println("Finish running the program"); return 1; } } }
5!- W##,-LE %BG52.'11'((,"!:(<1(0 %BsQt
5!.W##,-LE G%B 52.'11'((,"!:(<1(0 %BsQ
!" '0'' G0 1* ]zWZadVXU 0L/5+'60L/5 %B1='4"59"NO/'.&H6//:.K:(> }bX07B/1A5 C2/WA5-#'5CS8"9"1'((%$19I2='&,B6 /' A IH" X X C/6#'& FileInputStream
/'.&H6//:.KC/6#'& FileNotFoundException *2'G0H-+GJ#E5,61#H'@ 4(7/0L/5 getLocalHost() C/6#'& InetAddress /'.&H6//:.KC/6#'& UnknownHostException*2'G0H ('+QH,JJ08KK C/6(7B/6>'?'.'@'1='4"5942(' D2/6C%$"!:(<1(0.,51'(1,+C2/WA5-#'5 07B/0%1'((%$19I20L/5 %B/'.&H6//:.K:(> IOException&='4(,+0L/5RSB61'(.,51'(1,+C2/WA5-#'5<+H6//1:;" PQ 9I2='&,B6try/catch5,6 %B1#H'@G@29"4,@C2/ %BW'H "0' Q 9I2='&,B6 throws 9"1'(:(1')0L/5 .%B 0%1'((%$19I20L/595F %B/'.&H6 //:.K:(> }bX9"1(%"%840'$@'0@H'0L/5 %B:(1')G0HD2/61'( .,51'(1,+//:.K:(> }bX5,61#H'@/6
D,@/$H" 6IH"
publicvoid openFile(Strings)throwsFileNotFoundException { }
0L/595F&'0'(* .%B .,51'(1,+//:.K:(> }bX!5$9I2='&,6B throws G520'11@H'4"SB6I"A5D,@/$H'6IH" public void openFile(String s)throws FileNotFoundException, UnknownHostException { }
1(% %B0%1'(9I2='&,B6 throws <#2@&H6DH/9420L/5/7B"F %B (%$19I2:;"D,@.,51'(1,+ //:.K:(> }bX 5,61#H'@G:(7B/$F }bX 5,61#H'@ !:(<1(0.1A5C2/WA5-#'59"C,8"D/"1'((,"!:(<1(007B/0%C/2 WA5-#'5C/6//:.K:(> }bX5,61#H'@1A5CS8"D,@/$H'6IH"!:(<1(0 %BsQvRSB6W##,-LE %BG52.'11'((,":;"5,6<&56 9"(M: %BsQv
!"!#$%&'(&" )&*'+," !"!#$%-(./01#2'.2'3 4'(#'51(+,6
("' 5!3 D,@/$H'6 %BG0H01% '(.,51'(1,+//:.K:(> Exception public class ExceptionDemo1 { public static void main(String args[]) { ExceptionDemo1 ex1 = new ExceptionDemo1(); ex1.method1(); } public void method1() throws ArithmeticException { method2(); } public void method2() throws ArithmeticException { System.out.println(2/0); } }
5!3 W##,-LE %BG52.'11'((,"!:(<1(0 %BsQv ('&'0'(* %B.9I2='&,6B throws 9"0L/5 %BI7B/ main() G52 '?'.'@'G0HG52 1='4"5942('.D2/6C%$"='&,6B 9"1'(.,51'(1,+0L/5 %B/'.&H6C2/WA5-#'5C/6//:.K :(> RuntimeException IOException 0L/5 0%B %='&,6B throws .0%W#DH/1'(&7+ /5 ,86"%8 "7B/6.'11C/61'(1='4"5 0L/5<++ VV .G0H/"3n'D9420%1'(.,51'(//:.K:(> }bX !5$9I2 ='&,B6 throws 0'1I"A51@H' %B0L/55A0.,51'(/$MH !:(<1(0 %B sQz <#!:(<1(0 %B sQs <&56D,@/$H'6!:(<1(0 0%B %0L/5<++ VV %B*M1D2/6<#G0H*M1D2/6D'0#='5,+
("' 5!4 D,@/$H'6 0%B %0L/5<++=-800/JJ87 %B*M1D2/6 import java.io.*; public class Parent { public void myMethods() throws IOException { } } public class OverrideException extends Parent{ public void myMethods() throws IOException { new FileInputStream("temp.txt"); } }
("' 5!5 D,@/$H'6 0%B %0L/5<++=-800/JJ87 %BG0H*M1D2/6 import java.io.*; public class Parent { public void myMethods() throws FileNotFoundException } public class OverrideExceptionV2 extends Parent{ public void myMethods() throws FileNotFoundException,IOException new FileInputStream("temp.txt"); } }
{ }
{
!& ' #]zWZadVXURD& 1 NO0%#'&:(> }bX/$MH4#'$I"A5 }bX CS8"0'940H -7B/9I29"1'((+3C2/WA5-#'5|-'.'.6IH" !:(<1(0(++ +%$"",1)S1?'/'.0%#'& I%B 7B/ StudentIDNotFoundException -7B/9I29"1'(&(2'6//:.K %B.&H6C2/WA5-#'5 07B/G0H&'0'(*D(@.-+(4,&",1)S1?' %BD2/61'( 1'(&(2'6#'&:(> }bXCS8"0'940H &'0'(* ='G52!5$"A$'0#'&95F942&7+ /50' .'1#'& %BI7B/ Exception 9"1(% %BD2/61'(+,6,+9420L/595F.,51'(1,+//:.KC/6
!"!#$%&'(&" )&*'+," !"!#$%-(./01#2'.2'3 4'(#'51(+,6
#'&",8"9"1(% %B 1A5C2/WA5-#'5CS8" 4(7/"A$'0#'&95F942&+7 /50'.'1#'& Runtime Exception9"1(% %BG0HD2/61'(9420L/595F.=':;"D2/6.,51'(1,+C2/WA5-#'55,61#H'@ !5$ @,B G:#'& %BI7B/Exception.0%X X &/6(M:<++7/ -
public Exception() public Exception(String s)
5,6",8"#'& &%B 7+ /50'.'1#'& I%B 7B/ Exception @(.0% X X ,86&/6 !5$(M:<++4"SB6.0% %B0%I"A5C2/0M#:;" String -7B/(,+C2/@'0 .%B /LA+'$ C2/WA5-#'5<#='&,B6<(19" X X 5,61#H'@@(:;"='&,B6 super(s); -7B/&H6 C2/@'05,61#H'@9421,+X X C/6 X#'& I%B /7B Exception!:(<1(0 %B sQPm <&56D,@/$H'6C/6#'& MyOwnException RSB6:;"#'&:(> }bX %B 1='4"5CS8"940H ("' 5! 6 D,@/$H'6#'&:(> }bX %B1='4"5CS8"940H
public class MyOwnException extends Exception { public MyOwnException (String s) { super(s); } }
!& '+ G0 1* MkGJSCANI 0L/5 %BD2/61'(&H6//:.K:(> }bX 07B/1A5C2/WA5-#'5CS8"9"='&,B695 .D2/6(%$19I2='&,B6 I%B 7B/ throw -7B/.&(2'6//:.KC/6#'&:(> }bX CS8"0' !5$0%(:M <++='&,6B 5,6"%8 throw new ExceptionType([arguments])
!5$ %B
7/I7B/C/6#'&:(> }bX %BD2/61'(.&(2'6 //:.K!5$0% &/5#2/61,+ %B(+39"X X C/6#'&5,61#H'@ "/1.'1"%8='&,6B :(1')0L/5",8".D2/60%='&,B6 throws -7B/1='4"5942='&,B69" 0L/5/7B"F %B (%$19I20L/5"%8D/2 6C%$"='&,B69"1'(.,51'(1,+C2/WA5-#'5"%8 !:(<1(0 %B sQPP x
ExceptionType
<&56D,@/$H'61'(C%$"#'&FileHandler!5$0%0L/5openFile()RSB6.&H6//:.K C/6#'& MyOwnException CS8"0'07B/G0H-+GJ#E %B(+3 &H@"!:(<1(0 %B sQP <&56 D,@/$H'6!:(<1(0 %B0%1'(.,51'(1,+C2/WA5-#'55,61#H'@ ("' 5! D,@/$H'6#'&FileHandler import java.io.*; class FileHandler { public public static void openFile(String filename) throws MyOwnException File f = new File(filename); if (!f.exists()) { throw new MyOwnException("File Not Found"); } } } ("' 5! $ D,@/$H'6 %B0%1'(.,51'(1,+C2/WA5-#'5
{
public class FileOpener { public static void main(String args[]) { try { FileHandler.openFile(args[0]); System.out.println("Open successful"); } catch (MyOwnException ex) { System.out.println(ex); } } }
/& x C2/5%:(1'(4"SB6C/6>'?'.'@'7/ ('&'0'(*C%$"!:(<1(09420%1'(D(@..,+<# .,51'(1,+C2/WA5-#'5 %B/'.1A5CS8"G52!5$ %B1'( ='6'"G0HD2/64$35#6 x <00=0 :;"C2/WA5-#'5 %BG0H&'0'(*<12GC<#.,51'(G52 &H@"
!"!#$%&'(&" )&*'+," !"!#$%-(./01#2'.2'3 4'(#'51(+,6
x ='&,B6 try <# catch :;"='&,B6 9%B I29"1'(D(@..,+<#.,51'(1,+C2/WA5-#'5 %B /'.1A5CS8"G52 !5$+#K/1='&,6B catch&'0'(*0%G520'11@H'4"SB6+#K/1&='4(,+9" Exception I"A5940HCS8"G52 !5$.D2/6&7+ /50' .'1#'& Exception <#D2/60%1'((%$19I2 5=7K10251=0 C/6#'& Exception52@$
!"!#$%&'(&" )&*'+," !"!#$%-(./01#2'.2'3 4'(#'51(+,6