欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java基礎(chǔ)之異常處理操作示例

 更新時(shí)間:2019年08月24日 13:20:23   作者:考班格  
這篇文章主要介紹了Java基礎(chǔ)之異常處理操作,涉及java異常捕獲、拋出異常、自定義異常處理相關(guān)操作技巧,需要的朋友可以參考下

本文實(shí)例講述了Java基礎(chǔ)之異常處理操作。分享給大家供大家參考,具體如下:

示例代碼:

public class ExecDemo {
  public static void main(String[] args) {
    int[] nums = new int[4];
    System.out.println("before the exception:");
    try {  //try代碼塊  try catch代碼塊可以嵌套
      try{
         nums[7] = 10;  //數(shù)組越界
         System.out.println("no exception:");
      }catch(ArithmeticException e){
         e.printStackTrace();
      }
    }catch(ArithmeticException e) { //catch代碼塊  多個(gè)catch塊
      e.printStackTrace();  //打印異常信息
    }catch(ArrayIndexOutOfBoundsException e){  // 捕獲數(shù)組越界錯(cuò)誤  捕獲子類異常
       e.printStackTrace();
    }catch(Throwable e){  //捕獲超類異常 Throwable是所有異常的超類
       e.printStackTrace();
    }
    System.out.println("after the exception");
  }
}

拋出異常:

public class ThrowDemo {
  public static void main(String[] args) {
    try {
      System.out.println("before throw:");
      throw new ArithmeticException();  //throw關(guān)鍵字拋出一個(gè)ArithmeticException()異常(手動(dòng)拋出異常)
    } catch (ArithmeticException e) {  //捕獲異常
      System.out.println("exception caught:");
    }
    System.out.println("after try{}catch{}:");
  }
}

重新拋出異常:

class Rethrow {
  public static void genException() {
    int[] numer = {2,4,6,8,10,12};
    int[] demon = {2,0,3,4,0};
      for(int i=0;i < numer.length;i++){
        try {                                   //try代碼塊
          System.out.println(numer[i]/demon[i]);
        } catch (ArithmeticException e) {  //多個(gè)catch()塊
          System.out.println("can't dev by zero:");
        }catch(ArrayIndexOutOfBoundsException e) {
          System.out.println("no error:");
          throw e;  //throw 重寫拋出異常
        }
      }
  }
}
public class RethrowDemo{
  public static void main(String args[]) {
    try {
      Rethrow.genException();
    } catch (ArrayIndexOutOfBoundsException e) {  //捕獲重新拋出的異常
      System.out.println("error error error error error:");
    }
    finally{  //finally代碼塊在try catch執(zhí)行完時(shí)執(zhí)行的。
       System.out.println("Leaving try.");
     }
  }
}

throws語句:一個(gè)方法產(chǎn)生自己不做處理的異常,用throws拋出到外層(誰調(diào)用,誰處理異常)

public class ThrowsDemo {
  public static char prompt(String str) throws java.io.IOException{//prompt()方法產(chǎn)生自己不做處理的IOException異常,拋出到外層,誰調(diào)用誰處理異常
    System.out.print(str +":");
    return (char) System.in.read();
  }
  public static void main(String[] args) {
    char ch;
    try {
      ch = prompt("enter a letter");  //prompt()可能拋出異常,
    } catch (java.io.IOException e) {  //捕獲prompt()拋出的異常
      System.out.println("IOException occurred");
      ch = 'x';
    }
    System.out.println("you pressed:"+ ch);
  }
}

可以用一個(gè)catch()捕獲多個(gè)異常:

try{

}
catch(ArithmeticException|ArrayIndexOutOfBoundsException e){//同時(shí)捕獲多個(gè)異常
}

自定義異常:

class NonIntResultException extends Exception{  //自定義異常繼承子Exception
  int n ,d;
  NonIntResultException(int i,int j){
    n = i;
    d = j;
  }
  public String toString() {
    return "result of "+ n +"/"+ d +" is non-integer.";
  }
}
public class CustomExceptionDemo {
  public static void main(String[] args) {
    int numer[] = {4,8,15,32,64,127,256,512};
    int denom[] = {2,0,4,4,0,8};
    for(int i=0;i<numer.length;i++) {
      try {
        if((numer[i]%2)!=0) {
          throw new NonIntResultException(numer[i],denom[i]);  //拋出自定義異常
        }
        System.out.println(numer[i] +"/"+ denom[i] +" is "+ numer[i]/denom[i]);
      } catch (ArithmeticException e) {  //捕獲ArithmeticException異常
        System.out.println("can't divide by zero!");
      }catch (ArrayIndexOutOfBoundsException e) { //捕獲ArrayIndexOutOfBoundsException 異常
        System.out.println("no matching element found.");
      }catch (NonIntResultException e) {  //捕獲自定義異常
        System.out.println(e);
      }
    }
  }
}

更多java相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Java面向?qū)ο蟪绦蛟O(shè)計(jì)入門與進(jìn)階教程》、《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java操作DOM節(jié)點(diǎn)技巧總結(jié)》、《Java文件與目錄操作技巧匯總》和《Java緩存操作技巧匯總

希望本文所述對大家java程序設(shè)計(jì)有所幫助。

相關(guān)文章

  • Java中的CurrentHashMap源碼詳解

    Java中的CurrentHashMap源碼詳解

    這篇文章主要介紹了Java中的CurrentHashMap源碼詳解,HashMap是數(shù)組+鏈表構(gòu)成的,JDK1.8之后,加入了紅黑樹,HashMap默認(rèn)數(shù)組初始化大小為16,如果瞎設(shè)置數(shù)字,它會自動(dòng)調(diào)整成2的倍數(shù),需要的朋友可以參考下
    2023-12-12
  • 詳解spring中aop不生效的幾種解決辦法

    詳解spring中aop不生效的幾種解決辦法

    這篇文章主要介紹了詳解spring中aop不生效的幾種解決辦法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-06-06
  • 基于 SpringBoot 實(shí)現(xiàn) MySQL 讀寫分離的問題

    基于 SpringBoot 實(shí)現(xiàn) MySQL 讀寫分離的問題

    這篇文章主要介紹了基于 SpringBoot 實(shí)現(xiàn) MySQL 讀寫分離的問題,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-02-02
  • 20個(gè)非常實(shí)用的Java程序代碼片段

    20個(gè)非常實(shí)用的Java程序代碼片段

    這篇文章主要為大家分享了20個(gè)非常實(shí)用的Java程序片段,對java開發(fā)項(xiàng)目有所幫助,感興趣的小伙伴們可以參考一下
    2016-02-02
  • java讀取excel圖片導(dǎo)入代碼示例(親測有效)

    java讀取excel圖片導(dǎo)入代碼示例(親測有效)

    在日常工作中,我們經(jīng)常要將一些照片插入到Excel表格中,這篇文章主要給大家介紹了關(guān)于java讀取excel圖片導(dǎo)入的相關(guān)資料,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-10-10
  • java實(shí)現(xiàn)簡易的五子棋游戲

    java實(shí)現(xiàn)簡易的五子棋游戲

    這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)簡易的五子棋游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-06-06
  • Java 駝峰命名法詳解(必看篇)

    Java 駝峰命名法詳解(必看篇)

    下面小編就為大家?guī)硪黄狫ava 駝峰命名法詳解(必看篇)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-05-05
  • Spring Cloud中使用Eureka的詳細(xì)過程

    Spring Cloud中使用Eureka的詳細(xì)過程

    Eureka 是 Netflix 開源的一個(gè)服務(wù)發(fā)現(xiàn)組件,它在微服務(wù)架構(gòu)中扮演著重要的角色,這篇文章主要介紹了Spring Cloud中如何使用Eureka,需要的朋友可以參考下
    2024-07-07
  • Java常用集合與原理解析

    Java常用集合與原理解析

    這篇文章主要介紹了Java常用集合與原理解析,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-03-03
  • SpringBoot整合SpringSession實(shí)現(xiàn)分布式登錄詳情

    SpringBoot整合SpringSession實(shí)現(xiàn)分布式登錄詳情

    這篇文章主要介紹了SpringBoot整合SpringSession實(shí)現(xiàn)分布式登錄詳情,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的朋友可以參考一下
    2022-08-08

最新評論