1.1、異常定義
異常:--不正常,程序在運行時出現不正常情況
異常由來:其實也是現實生活中一個具體的事物,馬可以通過java的類的形式表現描述,并封裝成類。
Java對不正常情況描述后的,對象體現。
異常:兩種.
一種是嚴重的問題:java通過Error類進行描述
對于Error一般不編寫針對必的代碼對其進行處理
對與非嚴重的:java通過Exception類進行描述
對于Exception可以使用針對必的處理方式進行處理。
示例:
package com.day10.demo1;public class Demo1 { public static void main(String[] args) { System.out.PRintln(div(5,0)); System.out.println("over"); } public static int div(int a,int b){ return a/b; }}
結果:
Exception in thread "main" java.lang.ArithmeticException: / by zero
at com.day10.demo1.Demo1.div(Demo1.java:14)
at com.day10.demo1.Demo1.main(Demo1.java:7)
算術異常,因為除數為0,所以,有異常了
1.2、異常體系結構

Java 提供了兩類主要的異常:runtime exception 和checked exception。所有的checked exception 是從java.lang.Exception 類衍生出來的,而
runtime exception 則是從java.lang.RuntimeException 或java.lang.Error類衍生出來的。
它們的不同之處表現在兩方面:機制上和邏輯上
runtime exceptions:
checked exceptions:
從邏輯的角度來說,checked exceptions 和runtime exception 是有不同的使用目的的。
checked exception 用來指示一種調用方能夠直接處理的異常情況。checked exception 迫使你捕獲它并處理這種異常情況。
而runtime exception 則用來指示一種調用方本身無法處理或恢復的程序錯誤。
1.3、異常使用語句
try{ // 有可能出現異常的語句}catch(異常類 異常對象){ // 編寫異常的處理語句}[ catch(異常類 異常對象){ // 編寫異常的處理語句} catch(異常類 異常對象){ // 編寫異常的處理語句} …. ][finally{ 一定會運行到的程序代碼 ; }
主要搭配:try-catch、try-catch-finally、try-finally三種形式

1.4、使用try--catch
沒有使用異常
package com.day10.exception.demo1;public class Demo1 { public static void main(String[] args) { int x=5; int y=0; int index=div(x,y); System.out.println("執行完成"); } public static int div(int a,int b){ return a/b; }}
結果:
Exception in thread "main" java.lang.ArithmeticException: / by zero
最后輸出語句沒有執行
使用try--catch
package com.day10.exception.demo1;public class Demo1 { public static void main(String[] args) { int x=5; int y=0; try { int retsult=x/y; } catch (Exception e) { e.printStackTrace(); } System.out.println("執行完成"); } }
結果:
java.lang.ArithmeticException: / by zero
at com.day10.exception.demo1.Demo1.main(Demo1.java:10)
執行完成
異常代碼塊后面的代碼被執行了。
try-中的代碼發生異常后,跳轉到相就的 catch塊中,捕獲異常,完成后,執行異常塊后面的代碼
1.5、try-catch-finally
package com.day10.exception.demo1;public class Demo1 { public static void main(String[] args) { int x=5; int y=0; try { int retsult=x/y; System.out.println("執行到我說明沒有異常"); } catch (Exception e) { System.out.println("發生異常了"); e.printStackTrace(); }finally{ System.out.println("我一定會被執行"); } System.out.println("執行完成"); } }
結果:
發生異常了
我一定會被執行
執行完成
java.lang.ArithmeticException: / by zero
at com.day10.exception.demo1.Demo1.main(Demo1.java:10)
1.6、finally中的代碼不執行
package com.day10.exception.demo1;public class Demo1 { public static void main(String[] args) { int x=5; int y=0; try { int retsult=x/y; System.out.println("執行到我說明沒有異常"); } catch (Exception e) { System.out.println("發生異常了"); e.printStackTrace(); System.exit(0); //或者-1 }finally{ System.out.println("我一定會被執行"); } System.out.println("執行完成"); } }
發生異常了java.lang.ArithmeticException: / by zero at com.day10.exception.demo1.Demo1.main(Demo1.java:10)
1.7、如果有return 怎么執行
try中有return
package com.day10.exception.demo1;public class Demo2 { public static void main(String[] args) { System.out.println(getException()); } public static int getException(){ try { int x=5; System.out.println("try return"); return x; } catch (Exception e) { System.out.println("catch return"); }finally{ System.out.println("finally return"); } return 0; }}
結果:
try return
finally return
5
try中的return并不會影響finally中的代碼執行
catch中有return
package com.day10.exception.demo1;public class Demo2 { public static void main(String[] args) { System.out.println(getException()); } public static int getException(){ int x=5; try { int result=x/0; System.out.println("try return"); return x; } catch (Exception e) { System.out.println("catch return"); return ++x; }finally{ System.out.println("finally return"); } }}
結果:
catch return
finally return
6
catch中的return也不會影響finally中的執行,但是返回結果會是catch中的return結果,會將try中的結果覆蓋
finally中有return
package com.day10.exception.demo1;public class Demo2 { public static void main(String[] args) { System.out.println(getException()); } public static int getException(){ int x=5; try { int result=x/0; System.out.println("try return"); return x; } catch (Exception e) { System.out.println("catch return"); return ++x; }finally{ System.out.println("finally return"); return ++x; } }}
結果:
catch return
finally return
7
finally中的return也不會影響finally中的執行,但是返回結果會是finally中的return結果,會將try和catch中的結果覆蓋
1.8、多個catch塊
exception異常 一定要放在最扣,不然下方的異常無法執行到,只會執行父類執行而子類的異常不會被捕獲
package com.day10.exception.demo1;import java.util.InputMismatchException;import java.util.Scanner;/* * 求2個整數的商 當異常發生時,程序如何處理 */public class Demo2 { public static void main(String[] args) { // 從鍵盤獲得輸入 try { Scanner input = new Scanner(System.in); System.out.println("請輸入被除數:"); int num1 = input.nextInt(); System.out.println("請輸入除數:"); int num2 = input.nextInt(); int result = num1 / num2; System.out.println(num1 + "與" + num2 + "的商: " + result); } catch (InputMismatchException e) { System.out.println("輸入的不是數字"); e.printStackTrace(); } catch (ArithmeticException e) { System.out.println("除數不能為0"); e.printStackTrace(); } catch (Exception e) { System.out.println("其它錯誤!"); e.printStackTrace(); } finally { System.out.println("謝謝使用!"); } }}
結果1:
請輸入被除數:
fsd
輸入的不是數字
謝謝使用!
java.util.InputMismatchException
結果2
請輸入被除數:
9
請輸入除數:
0
除數不能為0
謝謝使用!
java.lang.ArithmeticException: / by zero
結果3:
請輸入被除數:
7
請輸入除數:
3
7與3的商: 2
謝謝使用!
1.9、自定義異常
自定義異常只需要繼承Exception類就
定義異常類,只需要繼承Exception類即可。
package com.day10.exception.demo1;/** * 自定義異常 * @author denny * */public class MyException extends Exception { public MyException() { super(); } public MyException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { super(message, cause, enableSuppression, writableStackTrace); } public MyException(String message, Throwable cause) { super(message, cause); } public MyException(String message) { super(message); } public MyException(Throwable cause) { super(cause); } }
上面為算定義異常,使用構造方法重載即可
1.10、throw和throws拋出異常
使用上面的算定義異常
package com.day10.exception.demo1;public class Person { private String name; private int age; private String gender; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { if (age >= 0 && age <= 150) { this.age = age; } else { try { throw new MyException("年齡只能在0-150歲之間"); } catch (MyException e) { e.printStackTrace(); } } } public String getGender() { return gender; } public void setGender(String gender) throws MyException {// if(gender.equals("男")||gender.equals("女")){ this.gender = gender; }else{ throw new MyException("性別只能是男或者女!"); /* throw可以在方法體內拋出異常,但必須捕獲或者 在方法體上用throws拋出 */ } } public void printSelf(){ System.out.println("姓名:"+this.name+" 性別:"+this.gender+" 年齡:"+this.age); }}
測試類
package com.day10.exception.demo1;public class PersonTest { public static void main(String[] args) { Person p=new Person(); try { p.setName("張三"); p.setAge(200); p.setGender("人妖"); } catch (MyException e) { e.getMessage();//獲取異常信息 }finally{ } }}
結果:com.day10.exception.demo1.MyException: 年齡只能在0-150歲之間
因為在執行age賦值時,已經發生異常,所以下面的性別賦沒有執行
package com.day10.exception.demo1;public class PersonTest { public static void main(String[] args) { Person p=new Person(); try { p.setName("張三"); p.setAge(20); p.setGender("人妖"); p.printSelf(); } catch (MyException e) { //System.out.println(e.getMessage());//獲取異常信息 e.printStackTrace(); }finally{ } }}
結果:
com.day10.exception.demo1.MyException: 性別只能是男或者女!
2.1、包的含義
對文件進行分類管理,類似于windows中的文件夾,同一包同一層
給類提供多層命名空間,同一個文件夾文件名不相同,層次關系,
寫在程序文件的第一行
類名的全稱是包名.類名.
包也是一種封裝形式
2.2、包的定義
package 包名,
包名必須小寫,可以使用.小數點來定義多層次包
必須放在代碼的第一行
javac -d . java文件名.java .代表當前文件夾目錄
包的出現可以把java的class文件和源文件相分離,只需要將 class文件復制出來運行就可以
定義包名不要重復
2.3、包的導入
import 包名.類名
2.4、包與包之間的訪問權限

新聞熱點
疑難解答