Java物件導向中的異常

來源:互聯網
上載者:User

標籤:alt   UNC   term   throws   虛擬機器   運行時異常   stack   溢出   條件   

Java中的異常和錯誤
Java 中的異常機制,更好地提升程式的健壯性

throwable 為頂級, Error 和 Exception

Error :虛擬機器錯誤,記憶體溢出,線程死結

Exception : RuntimeException 為空白指標異常,數組下標越界異常,算數異常,類型轉換異常等,IO異常( IOException ),SQL異常( SQLException )。

異常處理,在 Java 中異常處理機製為: 拋出異常 和 捕捉異常

異常的描述:

class ExceptionDemo{
public static void main(String[] args){
int[] arr = new int[3];
System.out.println(arr[0]);
// 結果 為 0
System.out.println(arr[3]);
// 結果 為 圖1
}
}

System.out.println(arr[3]); 編譯沒問題,文法沒有問題,編譯完記憶體中沒數組,啟動並執行時候才在堆記憶體中開闢數組空間。 arr[3] 沒有這個下標,所以在運行時找不到結果。

圖1,表示數組下標越界異常, System.out.println(arr[3]); 運行時發生了異常為 ArrayIndexOutOfBoundException ,導致了程式無法運行,程式終結,不在執行。

錯誤的描述

class ExceptionDemo{
public static void main(String[] args){
int[] arr = new int[102410241024];
System.out.println(arr[0]);
// 結果 為 0
System.out.println(arr[3]);
// 結果 為 圖1
}
}

圖2,表示運行時發生的錯誤,堆記憶體溢出。

異常和錯誤的區別
對於異常是由解決方案的,Java中提供了對應的處理機制,而錯誤沒有,是沒有辦法去針對性的解決,唯一方法就是錯誤出現,修改代碼。

異常的過程
在異常情況,運行時發生的問題,是數組下標越界異常,在異常拋出的問題為名稱,內容,發生的位置等,多種資訊進行了封裝到對象中。

ArrayIndexOutOfBoundsException

public class ArrayIndexOutOfBoundsException extends IndexOutOfBoundsException

構造方法

ArrayIndexOutOfBoundsException()
ArrayIndexOutOfBoundsException(int index)
ArrayIndexOutOfBoundsException(String s);

ArrayIndexOutOfBoundsException 可以 new 對象,有構造方法,就可以 new 對象。建立對象,如果遇到問題就拋出, new ArrayIndexOutOfBoundsException(index) 。

如何拋出呢?利用關鍵字 throw ,出現異常,在 JAVA 虛擬機器, jvm 中需要把問題拋出,給調用者 main ,主函數收到拋出的異常對象,但主函數沒有辦法處理,繼續拋出調用者 jvm , jvm 收到異常問題後,將異常資訊顯示在螢幕上。

異常概述(意外、例外)
什麼是異常呢?常見:

try-catch-finally
// try-catch-finally
public void method(){
try {
// 程式碼片段
// 產生異常的程式碼片段
}catch (異常類型 ex) {
// 對異常進行處理的程式碼片段
}finally{
// 程式碼片段
}
}

> throw> throwsthrows 聲明時要進行拋出的異常throw 要手動將產生的異常拋出> public void method() throws Exception1,Exception2,...,ExceptionN {>     // 產生異常代碼> }

// throw new IOException();
// 自己拋出的問題自己進行異常解決。
// 在拋出異常處,通過throws關鍵字標明異常類型
public void method() throws 異常類型{
// 代碼 拋出
throw new 異常類型();
}

自訂異常
異常鏈

異常處理分類為:

拋出異常
捕捉異常
簡單案例

public class Test{
public static void main(String[] args){
try{
String msg = redText(“C:\a.txt”);
}catch(PathNotExistException e){
// 進行處理
}
}
}

Throwable異常的頂級父類
異常 Exception 處理方式有兩種,一為 捕獲 ,二為 繼續拋出編譯時間的異常 。

RuntimeException 運行時異常,只有在啟動並執行時候才會出現,可以處理,也可以不處理。

自訂異常,可以自己定義異常,自己定義一個類,如果這個類繼承某個異常類,繼承的是 Exception 或其他異常,即定義了一個 編譯時間異常 ,如果繼承的是運行時異常 RuntimeException 或是它的子類,就定義了一個 運行時異常 。

Throwable 類是 Java 中所有錯誤或異常的超類,只有當對象是這個類的執行個體時,能通過虛擬機器或是 Java 中 throw 語句拋出。

Exception分為兩大類
非檢查異常( Unchecked Exception ):編譯器不要求強制處理異常
檢查異常( Checked Exception ):編譯器要求必須處理的異常,如 IO 異常等
捕獲異常

try
catch
finally

聲明異常,拋出異常

throws
throw

如果某方法出現了異常,卻是沒有能力的處理,可以在方法處用 throws 來聲明拋出異常,誰調用這個方法,誰就去處理這個異常。‘

public void method() throws Exception1,Exception2,...,ExceptionN {
// 異常的代碼
}

Java中的異常處理情況
JAVA 異常
> try...catch...finally 結構的使用方法

class Test{
public static void main(String args[]){

try{
int i = 1 / 0;
}
catch(Exception e){
e.printStackTrace();
}
finally{
System.out.println("finally");
}
System.out.println(5);
}
}

class Test{
public static void main(String args[]){
try{
Thread.sleep(1000);
}
catch(Exception e){
e.printStackTrace();
}
}
}

throw 和 throws 的作用區別:

class Person{
private int age;

public void setAge(int age) throws Exception{
if(age<0){
RuntimeException e = new RuntimeException("年齡不能小於0");
throw e;
}
this.age = age;
}
}

class Test{
public static void main(String args[]){
Person person = new Person();
try{
person.setAge(-1);
}
catch(Exception e){
System.out.println(e);
}
}
}

Error和Exception的區別
Error 是 Throwable 的子類,用於標記嚴重錯誤
Exception 是 Throwable 的子類,指示合理的程式想去 catch 的條件,非嚴重錯誤
try/catch的執行過程
如果出現異常,系統則會拋出一個異常,

進行捕捉( catch 操作),或在最後( finally )來進行處理。

throw和throws的區別
throws 出現在方法聲明上, throw 出現在方法體內。

異常分類
異常分類: 可查異常 , 運行時異常 和 錯誤

異常鏈
異常鏈為我們捕獲一個異常後再拋出另一個異常

one -> two -> three

Java物件導向中的異常

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.