際際滷

際際滷Share a Scribd company logo
弍舒弍仂从舒 亳从仍ム亠仆亳亶
丐亳仗 仂亳弍仂从

亳弍从亳 从仂仄仗亳仍亳亳

亠仄亠仆亳 于仗仂仍仆亠仆亳
(run-time errors,
exceptions,
亳从仍ム亠仆亳)

仂亞亳亠从亳亠 仂亳弍从亳

if (flag == 1) n=0;
else n=1;
val=1/n;

1
弍舒弍仂从舒 亳从仍ム亠仆亳亶
从仍ム亠仆亳亠 于 Java  仂 仂弍亠从
仆亠从仂仂仂亞仂 从仍舒舒, 从仂仂亶 仂仗亳于舒亠
亳从仍ム亳亠仍仆仂亠 仂仂礌亳亠, 于仂亰仆亳从亠亠 于
从舒从仂仄-仍亳弍仂 舒从亠 仗仂亞舒仄仄仆仂亞仂 从仂亟舒.
亳 于仂亰仆亳从仆仂于亠仆亳亳 亳从仍ム亠仆亳
亳仗仂仍仆ム舒 亳亠仄舒 Java 仂亰亟舒亠
仂弍亠从 从仍舒舒, 于磶舒仆仆仂亞仂 
亟舒仆仆仄 亳从仍ム亠仆亳亠仄. 亅仂 仂弍亠从
舒仆亳 亳仆仂仄舒亳 仂 于仂亰仆亳从亠亶
亳从仍ム亳亠仍仆仂亶 亳舒亳亳 (仂从舒
于仂亰仆亳从仆仂于亠仆亳, 仂仗亳舒仆亳亠 亳 .仗.)
仂亰仄仂亢仆舒 从舒从 舒于仂仄舒亳亠从舒 舒从 亳
仗仂亞舒仄仄仆舒 亞亠仆亠舒亳 亳从仍ム亠仆亳亶.
2
弍舒弍仂从舒 亳从仍ム亠仆亳亶
个仂仄舒 try-catch 弍仍仂从舒
try
{ // 弍仍仂从 从仂亟舒 }
catch (ExceptionType1 亠xOb1)
{ // 仂弍舒弍仂亳从 亳从仍ム亠仆亳亶 亳仗舒
ExceptionType1 }
[catch (ExceptionType2 亠xOb2)
{ // 仂弍舒弍仂亳从 亳从仍ム亠仆亳亶 亳仗舒
ExceptionType2 }]
[finally
{ //从仂亟, 从仂仂亶 于仗仂仍仆磳 仗亠亠亟
于仂亟仂仄 亳亰 弍仍仂从舒 try }]
3
弍舒弍仂从舒 亳从仍ム亠仆亳亶
个仂仄舒 try-catch 弍仍仂从舒
try
{ read_from_file ("data.txt");
calculate();
}
catch ( FileNotFoundException fe )
{ System.out.println("个舒亶仍 data.txt 仆亠 仆舒亶亟亠仆");
}
catch ( ArithmeticException aex )
{ System.out.println("亠仍亠仆亳亠 仆舒 仆仂仍");
}
4
弍舒弍仂从舒 亳从仍ム亠仆亳亶
个仂仄舒 try-catch 弍仍仂从舒
 仗舒于仍亠仆亳亠 仆亳从仂亞亟舒 仆亠 于仂亰于舒舒亠 亳亰 弍仍仂从舒 catch
仂弍舒仆仂 于 弍仍仂从 try, 仗仂仍亠 于仗仂仍仆亠仆亳 catch-弍仍仂从舒
仗舒于仍亠仆亳亠 仗亠亠亟舒亠 仂从亠, 仍亠亟ム亠亶 舒亰
仗仂仍亠 try-catch-弍仍仂从舒;
 仂弍仍舒 于亳亟亳仄仂亳 catch-弍仍仂从舒 仂亞舒仆亳亠仆舒
弍仍亳亢舒亶亳仄 仗亠亟亠于ム亳仄 于亠亢亟亠仆亳亠仄 try, .亠.
catch-弍仍仂从 仆亠 仄仂亢亠 亰舒于舒于舒 亳从仍ム亠仆亳亠,
于弍仂亠仆仆仂亠 束仆亠 于仂亳仄損 try-弍仍仂从仂仄;
 仂仗亠舒仂, 从仂仆仂仍亳亠仄亠 于亠亢亟亠仆亳亠仄 try,
亟仂仍亢仆 弍 仂从亢亠仆 亳亞仆仄亳 从仂弍从舒仄亳 亟舒亢亠
亠仍亳 仂 仂亟亳仆仂仆舒 亳仆从亳.
 弍仍仂从亳 try 仄仂亞 弍 于仍仂亢亠仆仆仄亳
5
弍舒弍仂从舒 亳从仍ム亠仆亳亶
弌亠从 于亰仂于仂于

仍亳 仂弍舒弍仂亳从 仆亠 仆舒亶亟亠仆 亳从仍ム亠仆亳亠 仗亠亠亟舒亠 JVM.
6
弍舒弍仂从舒 亳从仍ム亠仆亳亶
亠仂仍仆亶 仂弍舒弍仂亳从 亳从仍ム亠仆亳亶

7
弍舒弍仂从舒 亳从仍ム亠仆亳亶
仍舒 亳从仍ム亠仆亳亶

仆亳从仂亞亟舒 仆亠
仂弍舒弍舒于舒ム,
.从. 仆亠 亳仄亠亠 仄仍舒
仂弍磶舒亠仍仆仂
仂弍舒弍舒于舒, 亳仆舒亠
从仂亟 仆亠 从仂仄仗亳仍亳亠
仆舒亟仂 仂弍舒弍舒于舒, 仆仂
从仂仄仗亳仍仂 仂 仆亠 仗仂于亠磳
8
弍舒弍仂从舒 亳从仍ム亠仆亳亶
仍舒 亳从仍ム亠仆亳亶
Catch-弍仍仂从亳 仗仂仄舒亳于舒ム 于 仗仂磲从亠
亳 仗仂磦仍亠仆亳 于 仗仂亞舒仄仄亠, 仗亳 仂仄
仂弍舒弍仂亳从 catch 亟仍 仗亠从仍舒舒
仗亠亠于舒于舒亠 亳从仍ム亠仆亳 从舒从 亟仍 于仂亠亞仂
从仍舒舒 舒从 亳 亟仍 于亠 亠亞仂 仗仂亟从仍舒仂于.
弌仍亠亟仂于舒亠仍仆仂, 于 仗仂仍亠亟仂于舒亠仍仆仂亳
catch-弍仍仂从仂于 仗仂亟从仍舒 亳从仍ム亠仆亳亶
亟仂仍亢仆 仍亠亟仂于舒 仗亠亠亟 仍ミ英仄 亳亰
仗亠从仍舒仂于.

9
弍舒弍仂从舒 亳从仍ム亠仆亳亶
仍舒 亳从仍ム亠仆亳亶
try
{inputFile("data.txt");
calculate(); }
catch ( IOException ExObj2)
FileNotFoundException ExObj1)
{System.out.println("个舒亶仍 data.txt 仆亠 仆舒亶亟亠仆");
{System.out.println("亳弍从舒 于于仂亟舒-于于仂亟舒"); } }
Error:

catch ( FileNotFoundException ExObj1)
IOException ExObj2)

{System.out.println("亳弍从舒 于于仂亟舒-于于仂亟舒");
unreachable code! {System.out.println("个舒亶仍 data.txt 仆亠 仆舒亶亟亠仆");} }
.从. FileNotFoundException  仗仂亟从仍舒 IOException

10
弍舒弍仂从舒 亳从仍ム亠仆亳亶
仗亠舒仂 throw
仂亞舒仄仄仆舒 亞亠仆亠舒亳 亳从仍ム亠仆亳:
throw new <ExceptionClassName>();
throw new <ExceptionClassName>();

11
弍舒弍仂从舒 亳从仍ム亠仆亳亶
仗亠舒仂 throw
public void demoproc ()
{try
{throw new NullPointerException("demo"); }
catch (NullPointerException e)
{System.out.println("caught inside demoproc"); throw e; }
}
public static void main(String args[])
{try
{demoproc(); }
catch(NullPointerException e)
{System.out.println("recaught: " + e); }
}

12
弍舒弍仂从舒 亳从仍ム亠仆亳亶
仗亠舒仂 throw

于仂亟 仗仂亞舒仄仄:

13
弍舒弍仂从舒 亳从仍ム亠仆亳亶
仗亠舒仂 throws
从仍ム亠仆亳, 从仂仂亠 仗仂仂亢亟亠仆 仂 Exception, 仆仂 仆亠
仂 RuntimeException, 仄仂亞 弍 亞亠仆亠亳仂于舒仆
仂仍从仂 磦仆仂 仂仗亠舒亳亠亶 throw. 亳 仂仄 亠仍亳 仄亠仂亟
仄仂亢亠 于弍舒于舒 仂亟仆仂 亳亰 舒从亳 亳从仍ム亠仆亳亶, 仂
亟仂仍亢仆仂 于仗仂仍仆 仂亟仆仂 亳亰 亟于 仍仂于亳亶: 仍亳弍仂 亟仍
舒从仂亞仂 亳从仍ム亠仆亳 亟仂仍亢亠仆 弍 catch-仂弍舒弍仂亳从,
仍亳弍仂 于 亰舒亞仂仍仂于从亠 舒从仂亞仂 仄亠仂亟舒 亟仂仍亢仆舒 仂
从仂仆从亳:
throws <ExceptionClassName>
(.仆. Catch or Specify Requirement)
public String readLine() throws IOException
14
弍舒弍仂从舒 亳从仍ム亠仆亳亶
仗亠舒仂 throws
亰仂于 仄亠仂亟舒, 于 仂仗亳舒仆亳亳 从仂仂仂亞仂 仂亳
" throws ... ", 仂亢亠 亟仂仍亢亠仆 仆舒仂亟亳
仍亳弍仂
于仆亳 try-catch-弍仍仂从舒, 仍亳弍仂 于仆亳
仄亠仂亟舒  从仂仆从亳亠亶 " throws ... " 于 亠亞仂
亰舒亞仂仍仂于从亠 亳 .亟. 于仗仍仂 亟仂 仄亠仂亟舒 main().
丐舒从亳仄 仂弍舒亰仂仄, 亞亟亠-仂 于 仗仂亞舒仄仄亠 仍ミ頴笑
于仂亰仄仂亢仆仂亠 亳从仍ム亠仆亳亠, 仗仂仗舒亟舒ム亠亠 于
从舒亠亞仂亳 Catch or Specify Requirement
仂弍磶舒仆仂 弍 仗亠亠于舒亠仆仂 亳 仂弍舒弍仂舒仆仂.

15
弍舒弍仂从舒 亳从仍ム亠仆亳亶
仂亟从仍舒 Exception
class MyException extends Exception
{private int detail;
MyException(int a)
{detail = a; }
public String toString()
{return "MyException[" + detail + "]"; }
}
仍亳 仆舒亟仂 仂亰亟舒 亳从仍ム亠仆亳亠 仆亠仂弍磶舒亠仍仆仂亠 从 仗亠亠于舒 
亠亞仂 仆舒亟仂 仆舒仍亠亟仂于舒 仂 RuntimeException.
16

More Related Content

4. java lecture exceptions

  • 1. 弍舒弍仂从舒 亳从仍ム亠仆亳亶 丐亳仗 仂亳弍仂从 亳弍从亳 从仂仄仗亳仍亳亳 亠仄亠仆亳 于仗仂仍仆亠仆亳 (run-time errors, exceptions, 亳从仍ム亠仆亳) 仂亞亳亠从亳亠 仂亳弍从亳 if (flag == 1) n=0; else n=1; val=1/n; 1
  • 2. 弍舒弍仂从舒 亳从仍ム亠仆亳亶 从仍ム亠仆亳亠 于 Java 仂 仂弍亠从 仆亠从仂仂仂亞仂 从仍舒舒, 从仂仂亶 仂仗亳于舒亠 亳从仍ム亳亠仍仆仂亠 仂仂礌亳亠, 于仂亰仆亳从亠亠 于 从舒从仂仄-仍亳弍仂 舒从亠 仗仂亞舒仄仄仆仂亞仂 从仂亟舒. 亳 于仂亰仆亳从仆仂于亠仆亳亳 亳从仍ム亠仆亳 亳仗仂仍仆ム舒 亳亠仄舒 Java 仂亰亟舒亠 仂弍亠从 从仍舒舒, 于磶舒仆仆仂亞仂 亟舒仆仆仄 亳从仍ム亠仆亳亠仄. 亅仂 仂弍亠从 舒仆亳 亳仆仂仄舒亳 仂 于仂亰仆亳从亠亶 亳从仍ム亳亠仍仆仂亶 亳舒亳亳 (仂从舒 于仂亰仆亳从仆仂于亠仆亳, 仂仗亳舒仆亳亠 亳 .仗.) 仂亰仄仂亢仆舒 从舒从 舒于仂仄舒亳亠从舒 舒从 亳 仗仂亞舒仄仄仆舒 亞亠仆亠舒亳 亳从仍ム亠仆亳亶. 2
  • 3. 弍舒弍仂从舒 亳从仍ム亠仆亳亶 个仂仄舒 try-catch 弍仍仂从舒 try { // 弍仍仂从 从仂亟舒 } catch (ExceptionType1 亠xOb1) { // 仂弍舒弍仂亳从 亳从仍ム亠仆亳亶 亳仗舒 ExceptionType1 } [catch (ExceptionType2 亠xOb2) { // 仂弍舒弍仂亳从 亳从仍ム亠仆亳亶 亳仗舒 ExceptionType2 }] [finally { //从仂亟, 从仂仂亶 于仗仂仍仆磳 仗亠亠亟 于仂亟仂仄 亳亰 弍仍仂从舒 try }] 3
  • 4. 弍舒弍仂从舒 亳从仍ム亠仆亳亶 个仂仄舒 try-catch 弍仍仂从舒 try { read_from_file ("data.txt"); calculate(); } catch ( FileNotFoundException fe ) { System.out.println("个舒亶仍 data.txt 仆亠 仆舒亶亟亠仆"); } catch ( ArithmeticException aex ) { System.out.println("亠仍亠仆亳亠 仆舒 仆仂仍"); } 4
  • 5. 弍舒弍仂从舒 亳从仍ム亠仆亳亶 个仂仄舒 try-catch 弍仍仂从舒 仗舒于仍亠仆亳亠 仆亳从仂亞亟舒 仆亠 于仂亰于舒舒亠 亳亰 弍仍仂从舒 catch 仂弍舒仆仂 于 弍仍仂从 try, 仗仂仍亠 于仗仂仍仆亠仆亳 catch-弍仍仂从舒 仗舒于仍亠仆亳亠 仗亠亠亟舒亠 仂从亠, 仍亠亟ム亠亶 舒亰 仗仂仍亠 try-catch-弍仍仂从舒; 仂弍仍舒 于亳亟亳仄仂亳 catch-弍仍仂从舒 仂亞舒仆亳亠仆舒 弍仍亳亢舒亶亳仄 仗亠亟亠于ム亳仄 于亠亢亟亠仆亳亠仄 try, .亠. catch-弍仍仂从 仆亠 仄仂亢亠 亰舒于舒于舒 亳从仍ム亠仆亳亠, 于弍仂亠仆仆仂亠 束仆亠 于仂亳仄損 try-弍仍仂从仂仄; 仂仗亠舒仂, 从仂仆仂仍亳亠仄亠 于亠亢亟亠仆亳亠仄 try, 亟仂仍亢仆 弍 仂从亢亠仆 亳亞仆仄亳 从仂弍从舒仄亳 亟舒亢亠 亠仍亳 仂 仂亟亳仆仂仆舒 亳仆从亳. 弍仍仂从亳 try 仄仂亞 弍 于仍仂亢亠仆仆仄亳 5
  • 6. 弍舒弍仂从舒 亳从仍ム亠仆亳亶 弌亠从 于亰仂于仂于 仍亳 仂弍舒弍仂亳从 仆亠 仆舒亶亟亠仆 亳从仍ム亠仆亳亠 仗亠亠亟舒亠 JVM. 6
  • 8. 弍舒弍仂从舒 亳从仍ム亠仆亳亶 仍舒 亳从仍ム亠仆亳亶 仆亳从仂亞亟舒 仆亠 仂弍舒弍舒于舒ム, .从. 仆亠 亳仄亠亠 仄仍舒 仂弍磶舒亠仍仆仂 仂弍舒弍舒于舒, 亳仆舒亠 从仂亟 仆亠 从仂仄仗亳仍亳亠 仆舒亟仂 仂弍舒弍舒于舒, 仆仂 从仂仄仗亳仍仂 仂 仆亠 仗仂于亠磳 8
  • 9. 弍舒弍仂从舒 亳从仍ム亠仆亳亶 仍舒 亳从仍ム亠仆亳亶 Catch-弍仍仂从亳 仗仂仄舒亳于舒ム 于 仗仂磲从亠 亳 仗仂磦仍亠仆亳 于 仗仂亞舒仄仄亠, 仗亳 仂仄 仂弍舒弍仂亳从 catch 亟仍 仗亠从仍舒舒 仗亠亠于舒于舒亠 亳从仍ム亠仆亳 从舒从 亟仍 于仂亠亞仂 从仍舒舒 舒从 亳 亟仍 于亠 亠亞仂 仗仂亟从仍舒仂于. 弌仍亠亟仂于舒亠仍仆仂, 于 仗仂仍亠亟仂于舒亠仍仆仂亳 catch-弍仍仂从仂于 仗仂亟从仍舒 亳从仍ム亠仆亳亶 亟仂仍亢仆 仍亠亟仂于舒 仗亠亠亟 仍ミ英仄 亳亰 仗亠从仍舒仂于. 9
  • 10. 弍舒弍仂从舒 亳从仍ム亠仆亳亶 仍舒 亳从仍ム亠仆亳亶 try {inputFile("data.txt"); calculate(); } catch ( IOException ExObj2) FileNotFoundException ExObj1) {System.out.println("个舒亶仍 data.txt 仆亠 仆舒亶亟亠仆"); {System.out.println("亳弍从舒 于于仂亟舒-于于仂亟舒"); } } Error: catch ( FileNotFoundException ExObj1) IOException ExObj2) {System.out.println("亳弍从舒 于于仂亟舒-于于仂亟舒"); unreachable code! {System.out.println("个舒亶仍 data.txt 仆亠 仆舒亶亟亠仆");} } .从. FileNotFoundException 仗仂亟从仍舒 IOException 10
  • 11. 弍舒弍仂从舒 亳从仍ム亠仆亳亶 仗亠舒仂 throw 仂亞舒仄仄仆舒 亞亠仆亠舒亳 亳从仍ム亠仆亳: throw new <ExceptionClassName>(); throw new <ExceptionClassName>(); 11
  • 12. 弍舒弍仂从舒 亳从仍ム亠仆亳亶 仗亠舒仂 throw public void demoproc () {try {throw new NullPointerException("demo"); } catch (NullPointerException e) {System.out.println("caught inside demoproc"); throw e; } } public static void main(String args[]) {try {demoproc(); } catch(NullPointerException e) {System.out.println("recaught: " + e); } } 12
  • 14. 弍舒弍仂从舒 亳从仍ム亠仆亳亶 仗亠舒仂 throws 从仍ム亠仆亳, 从仂仂亠 仗仂仂亢亟亠仆 仂 Exception, 仆仂 仆亠 仂 RuntimeException, 仄仂亞 弍 亞亠仆亠亳仂于舒仆 仂仍从仂 磦仆仂 仂仗亠舒亳亠亶 throw. 亳 仂仄 亠仍亳 仄亠仂亟 仄仂亢亠 于弍舒于舒 仂亟仆仂 亳亰 舒从亳 亳从仍ム亠仆亳亶, 仂 亟仂仍亢仆仂 于仗仂仍仆 仂亟仆仂 亳亰 亟于 仍仂于亳亶: 仍亳弍仂 亟仍 舒从仂亞仂 亳从仍ム亠仆亳 亟仂仍亢亠仆 弍 catch-仂弍舒弍仂亳从, 仍亳弍仂 于 亰舒亞仂仍仂于从亠 舒从仂亞仂 仄亠仂亟舒 亟仂仍亢仆舒 仂 从仂仆从亳: throws <ExceptionClassName> (.仆. Catch or Specify Requirement) public String readLine() throws IOException 14
  • 15. 弍舒弍仂从舒 亳从仍ム亠仆亳亶 仗亠舒仂 throws 亰仂于 仄亠仂亟舒, 于 仂仗亳舒仆亳亳 从仂仂仂亞仂 仂亳 " throws ... ", 仂亢亠 亟仂仍亢亠仆 仆舒仂亟亳 仍亳弍仂 于仆亳 try-catch-弍仍仂从舒, 仍亳弍仂 于仆亳 仄亠仂亟舒 从仂仆从亳亠亶 " throws ... " 于 亠亞仂 亰舒亞仂仍仂于从亠 亳 .亟. 于仗仍仂 亟仂 仄亠仂亟舒 main(). 丐舒从亳仄 仂弍舒亰仂仄, 亞亟亠-仂 于 仗仂亞舒仄仄亠 仍ミ頴笑 于仂亰仄仂亢仆仂亠 亳从仍ム亠仆亳亠, 仗仂仗舒亟舒ム亠亠 于 从舒亠亞仂亳 Catch or Specify Requirement 仂弍磶舒仆仂 弍 仗亠亠于舒亠仆仂 亳 仂弍舒弍仂舒仆仂. 15
  • 16. 弍舒弍仂从舒 亳从仍ム亠仆亳亶 仂亟从仍舒 Exception class MyException extends Exception {private int detail; MyException(int a) {detail = a; } public String toString() {return "MyException[" + detail + "]"; } } 仍亳 仆舒亟仂 仂亰亟舒 亳从仍ム亠仆亳亠 仆亠仂弍磶舒亠仍仆仂亠 从 仗亠亠于舒 亠亞仂 仆舒亟仂 仆舒仍亠亟仂于舒 仂 RuntimeException. 16