异常处理 5.1Java异常处理的基础知识 5.1.1错误与异常 5.1.1异常处理机制 1抛出异常2捕获异常3.异常处理的类层次 52异常类的产生、捕获与处理 53异常的分类 54物出异常 55自定义异常类
异常处理 • 5.1 Java异常处理的基础知识 5.1.1 错误与异常 5.1.1 异常处理机制 1.抛出异常 2.捕获异常 3.异常处理的类层次 • 5.2 异常类的产生、捕获与处理 • 5.3 异常的分类 • 5.4 抛出异常 • 5.5 自定义异常类
5,2异常类的产生、捕获与处理 1异常的产生 2使用try- catch- finally语句捕获与处理异常 3. Exception异常类的方法 public class Try l public static void main(String args) int i=0 inta={5,6,7,8} for(i=0;1<5;i++) System. out printIn ("a[++=+) ystem out. print("3/0=+(3/0
5.2 异常类的产生、捕获与处理 1.异常的产生 2.使用try-catch-finally语句捕获与处理异常 3. Exception异常类的方法 public class Try1 { public static void main (String args[]) { int i=0; int a[] = {5,6,7,8}; for(i=0;i<5;i++) System.out.println(" a["+i+"]="+a[i]); System.out.print("3/0="+(3/0)); } }
ArraylndexoutofBounds exception D: myiava>javac Try l java D: myjava>java Try l a[0]=5 a a21=7 a[3]8 Exception in thread"main"java. lang. ArraylndexoutofBoundsException at Try l. main(Try l java: 8) D: myjava>javac Try1 java D: myjava>java Tryl Exception in thread"main"java. lang Arithmetic Exception: /by zero at Tryl. main(try l java: 9)
ArrayIndexOutOfBoundsException D:\myjava>javac Try1.java D:\myjava>java Try1 a[0]=5 a[1]=6 a[2]=7 a[3]=8 Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException at Try1.main(Try1.java:8) D:\myjava>javac Try1.java D:\myjava>java Try1 Exception in thread "main" java.lang.ArithmeticException: / by zero at Try1.main(try1.java:9)
使用 try-catch- finally语句捕获与 处理异常 catch(Exception Type e) finally <语句3
使用try-catch-finally语句捕获与 处理异常 try { } catch(ExceptionType e) { } finally { }
public class Trv2 i public static void main(String args) i int i=0 inta={5,6,78} for(i=0,1<5;i++) i System. out. print("a[+i+]+i+=+(ali) catch(ArrayIndexOutofBounds Exception System.out. print'"捕获数组下标越界异常!"), catch(Arithmetic Exception e System. out. print("捕获算术异常!") catch(Exception e) System. out. print("捕获"+e: getMessageo+"异常!") ∥l示异常信息 finally System. out printIn(" finally F="+i);捕获算术异常! finally I==0 a[11=6 finally F=I a2)/2=3 finally 1=2 System. out. println("继续!"), a3/3=2 finally 1=3 捕获数组下标越界异常! finally i=4 继续
public class Try2 { public static void main (String args[]) { int i=0; int a[] = {5,6,7,8}; for(i=0;i<5;i++) { try { System.out.print("a["+i+"]/"+i+"="+(a[i]/i)); } catch(ArrayIndexOutOfBoundsException e) { System.out.print("捕获数组下标越界异常!"); } catch(ArithmeticException e) {System.out.print("捕获算术异常!"); } catch(Exception e) { System.out.print("捕获"+e.getMessage()+"异常!"); } //显示异常信息 finally { System.out.println(" finally i="+i); } } System.out.println("继续!"); } } 捕获算术异常! finally i=0 a[1]/1=6 finally i=1 a[2]/2=3 finally i=2 a[3]/3=2 finally i=3 捕获数组下标越界异常! finally i=4 继续!
public class Try2 i public static void main(String args i int F=0 inta={56,7,8} for(i=0,ijava Try 2 i System. out. printIn("finally I=+1) 捕获/ by zero异常! finally i=0 a[l/1=6 finally F= a[2) 2=3 finally i=2 System.out. println("继续!") a3/3=2 finally 13 捕获nul异常! finally i=4 继续
public class Try2 { public static void main (String args[]) { int i=0; int a[] = {5,6,7,8}; for(i=0;ijava Try2 捕获/ by zero异常! finally i=0 a[1]/1=6 finally i=1 a[2]/2=3 finally i=2 a[3]/3=2 finally i=3 捕获null异常! finally i=4 继续!
53异常的分类 1.异常类层次图 程序对错误与异常的三种处理方式 ①程序不能处理的错误 ②程序应避免而不捕获的异常 ③程序必须捕获的异常 例53文件没有找到异常类 2.常见的公用异常类
5.3 异常的分类 • 1. 异常类层次图 程序对错误与异常的三种处理方式: ① 程序不能处理的错误 ② 程序应避免而不捕获的异常 ③ 程序必须捕获的异常 例5.3 文件没有找到异常类 • 2. 常见的公用异常类
Import java.10.*. public class Try3 public static void main(String args) FileInputStream fis= new FileInputStream("autoexec. bat") System. out. printIn ("I can not found this file! ") D: \myjava>javac Try3 java Try3. java: 6: unreported exception java. io. FileNot FoundException, must be caught or declared to be thrown FilelnputStream fis=new FileInputStream(autoexec. bat"); error D: myjava>
import java.io.*; public class Try3 { public static void main (String args[]) { FileInputStream fis = new FileInputStream("autoexec.bat"); System.out.println("I can not found this file!"); } } D:\myjava>javac Try3.java Try3.java:6: unreported exception java.io.FileNotFoundException; must be caught or declared to be thrown FileInputStream fis = new FileInputStream("autoexec.bat"); ^ 1 error D:\myjava>
54抛出异常 public class Try 4 public void run( byte k)∥求k的阶乘 byte y=1, 1, for(=1;i<=k;i++) 3!=6 y=(byte)y*1) System. out. println(k+!=+y) 4!=24 5!=120 public static void main(String args) 6!=-48 Try 4 a=new Try40 7!=-80 for(byte Fl <10; 1++) a run(1) 8!=-128 9!=-128
5.4 抛出异常 public class Try4 { public void run(byte k) //求k的阶乘 { byte y=1,i; for (i=1;i<=k;i++) y = (byte)(y * i); System.out.println(k+"!="+y); } public static void main (String args[]) { Try4 a = new Try4(); for (byte i=1;i<10;i++) a.run(i); } } 1!=1 2!=2 3!=6 4!=24 5!=120 6!=-48 7!=-80 8!=-128 9!=-128
1使用 throw语句抛出异常 public class Try5 public void run(byte k) byte y=l, F=1; System. out print(k+!=) for(i=l; KByte. MAX VALUE/i) Integer类的常量 throw new Exception(" overflow");∥溢出时抛出异常,12行 (byte)(*i)
1.使用throw语句抛出异常 public class Try5 { public void run(byte k) { byte y=1,i=1; System.out.print(k+"!="); for (i=1;iByte.MAX_VALUE/i) //Integer类的常量 throw new Exception(“overflow”); //溢出时抛出异常,12行 else y = (byte) (y * i); }