国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - Java異常處理的五個關鍵字

Java異常處理的五個關鍵字

2021-04-09 11:12彬菌 Java教程

本篇文章給大家詳細講述了關于Java異常處理的相關知識點,并列舉了5個重要關鍵字,一起啊參考學下。

異常:異常有的是因為用戶錯誤引起,有的是程序錯誤引起的,還有其它一些是因為物理錯誤引起的。

異常處理關鍵字:try、catch、finally、throw、throws

注意事項:

  1. 錯誤不是異常,而是脫離程序員控制的問題。
  2. 所有的異常類是從 java.lang.Exception 類繼承的子類。
  3. 異常類有兩個主要的子類:IOException 類和 RuntimeException 類。
  4. Java有很多的內置異常類。

異常大致分類:

  1. 用戶輸入了非法數據。
  2. 要打開的文件不存在。
  3. 網絡通信時連接中斷,或者JVM內存溢出。

語法:

?
1
2
3
try{
//需要監聽的代碼塊
}
?
1
2
3
4
5
catch(異常類型 異常名稱/e){
//對捕獲到try監聽到的出錯的代碼塊進行處理
throw 異常名稱/e; //thorw表示拋出異常
throw new 異常類型(“自定義”);
}

 

?
1
2
3
4
5
6
finally{
//finally塊里的語句不管異常是否出現,都會被執行
}
修飾符 返回值 方法名 () throws 異常類型{ //throws只是用來聲明異常,是否拋出由方法調用者決定
//代碼塊
}

代碼例子:(try與catch與finally)

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public class ExceptionTest {
    public static void main(String[] args) {
        Scanner input=new Scanner(System.in);
    try{ //監聽代碼塊
    int a=input.nextInt();
    int b=input.nextInt();
    double sum=a/b; 
    System.out.println(sum);
    }
    catch(InputMismatchException e){
      System.out.println("只能輸入數字");
    }
    catch(ArithmeticException e){
      System.out.println("分母不能為0");
    }
    catch(Exception e){ //Exception是所有異常的父類
      System.out.println("發生了其他異常");
    }
    finally{ //不管是否出現異常,finally一定會被執行
      System.out.println("程序結束");
    }
    }
}

代碼例子:(throw關鍵字)

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import java.util.InputMismatchException;
import java.util.Scanner;
 
public class ExceptionTest {
    public static void main(String[] args) {
        Scanner input=new Scanner(System.in);
    try{ //監聽代碼塊
    int a=input.nextInt();
    int b=input.nextInt();
    double sum=a/b; 
    System.out.println(sum);
    }
    catch(InputMismatchException e){ //catch(異常類型 異常名稱)
      System.out.println("只能輸入數字");
      throw e; //拋出catch捕捉到的異常
      //throw new InputMismatchException(); 同上
    }
    catch(ArithmeticException e){
      System.out.println("分母不能為0");
      throw new ArithmeticException("分母為0拋出異常"); //拋出ArithmeticException異常
    }
    catch(Exception e){ //Exception是所有異常的父類
      System.out.println("發生了其他異常");
    }
    finally{ //不管是否出現異常,finally一定會被執行
      System.out.println("程序結束");
    
    }
}

代碼例子:(throws)

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
public class Throws {
    int a=1;
    int b=0;
    public void out() throws ArithmeticException{ //聲明可能要拋出的異常,可以有多個異常,逗號隔開
        try{ //監聽代碼塊
        int sum=a/b;
        System.out.println(sum);
        }
        catch(ArithmeticException e){
            System.out.println("分母不能為0");
        }
        finally{ //不管是否出現異常,finally一定會被執行
            System.out.println("程序結束");
        }
    }
    public static void main(String[] args){
        Throws t=new Throws();
            t.out(); //調用方法
            throw new ArithmeticException("分母為0拋出異常"); //由調用的方法決定是否要拋出異常
            /*
             * 第二種拋出方式
             */
//          ArithmeticException a=new ArithmeticException("分母為0拋出異常");
//          throw a;
    }
}

原文鏈接:https://www.idaobin.com/archives/609.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 日本高清视频在线播放 | 在线视频一区二区三区 | 久久精品国产一区 | 91久久久久久久久久 | 亚洲欧美在线视频 | 欧美日韩一区精品 | 美日韩视频 | 国产精品原创巨作av | 欧美一区二区三区 | 成人羞羞视频在线观看免费 | 尤物视频在线观看 | 夜夜超碰 | 国产福利视频在线观看 | 欧美一级二级视频 | 成人高清视频在线观看 | 黄色小视频在线观看 | 亚洲一区 中文字幕 | a免费网站 | 欧美另类视频在线 | 亚洲二区在线观看 | 水密桃av| 免费成人在线观看视频 | 亚洲www视频| 亚洲成人综合网站 | 99热热热 | 日韩欧美网 | 亚洲成人精品久久久 | 91精品福利少妇午夜100集 | 激情欧美日韩一区二区 | 91精品国产乱码久久久久久 | 伊人久久国产 | 黄色美女网站 | 国产免费久久 | 久久精品欧美 | 亚洲精品电影在线观看 | 欧美一区二区三区 | 欧美日韩一区二区三区在线观看 | 在线一级视频 | 久久精品国产99 | 久久久五月天 | 久色视频在线观看 |