欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

java異常處理的關(guān)鍵字有哪些?怎么用?

java異常處理的關(guān)鍵字有哪些?怎么用?這些問(wèn)題可能是我們?nèi)粘9ぷ鲿?huì)見(jiàn)到的。通過(guò)這些問(wèn)題,希望你能收獲更多。下面是揭開(kāi)這些問(wèn)題的詳細(xì)內(nèi)容。

創(chuàng)新互聯(lián)建站是一家專(zhuān)注于成都做網(wǎng)站、成都網(wǎng)站設(shè)計(jì)、成都外貿(mào)網(wǎng)站建設(shè)與策劃設(shè)計(jì),廉江網(wǎng)站建設(shè)哪家好?創(chuàng)新互聯(lián)建站做網(wǎng)站,專(zhuān)注于網(wǎng)站建設(shè)十載,網(wǎng)設(shè)計(jì)領(lǐng)域的專(zhuān)業(yè)建站公司;建站業(yè)務(wù)涵蓋:廉江等地區(qū)。廉江做網(wǎng)站價(jià)格咨詢:18980820575

什么是異常?

異常有的是因?yàn)橛脩翦e(cuò)誤引起,有的是程序錯(cuò)誤引起的,還有其它一些是因?yàn)槲锢礤e(cuò)誤引起的。

異常處理關(guān)鍵字:

try、catch、finally、throw、throws

注意事項(xiàng):

1、錯(cuò)誤不是異常,而是脫離程序員控制的問(wèn)題。

2、所有的異常類(lèi)是從 java.lang.Exception 類(lèi)繼承的子類(lèi)。

3、異常類(lèi)有兩個(gè)主要的子類(lèi):IOException 類(lèi)和 RuntimeException 類(lèi)。

4、Java有很多的內(nèi)置異常類(lèi)。

語(yǔ)法:

try{
//需要監(jiān)聽(tīng)的代碼塊
}
catch(異常類(lèi)型 異常名稱(chēng)/e){
//對(duì)捕獲到try監(jiān)聽(tīng)到的出錯(cuò)的代碼塊進(jìn)行處理
throw 異常名稱(chēng)/e; //thorw表示拋出異常
throw new 異常類(lèi)型(“自定義”);
}
finally{
//finally塊里的語(yǔ)句不管異常是否出現(xiàn),都會(huì)被執(zhí)行
}
修飾符 返回值 方法名 () throws 異常類(lèi)型{  
//throws只是用來(lái)聲明異常,是否拋出由方法調(diào)用者決定
//代碼塊
}

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

public class ExceptionTest {
	public static void main(String[] args) {
		Scanner input=new Scanner(System.in);  
        try{ //監(jiān)聽(tīng)代碼塊  
        int a=input.nextInt();  
        int b=input.nextInt();  
        double sum=a/b;   
        System.out.println(sum);  
        }  
        catch(InputMismatchException e){  
            System.out.println("只能輸入數(shù)字");  
        }  
        catch(ArithmeticException e){  
            System.out.println("分母不能為0");  
        }  
        catch(Exception e){ //Exception是所有異常的父類(lèi)  
            System.out.println("發(fā)生了其他異常");  
        }  
        finally{ //不管是否出現(xiàn)異常,finally一定會(huì)被執(zhí)行  
            System.out.println("程序結(jié)束");  
        }  
	}
}

示例代碼:(throw關(guān)鍵字)

import java.util.InputMismatchException;
import java.util.Scanner;
 
public class ExceptionTest {
	public static void main(String[] args) {
		Scanner input=new Scanner(System.in);  
        try{ //監(jiān)聽(tīng)代碼塊  
        int a=input.nextInt();  
        int b=input.nextInt();  
        double sum=a/b;   
        System.out.println(sum);  
        }  
        catch(InputMismatchException e){ //catch(異常類(lèi)型 異常名稱(chēng))  
            System.out.println("只能輸入數(shù)字");  
            throw e; //拋出catch捕捉到的異常  
            //throw new InputMismatchException(); 同上  
        }  
        catch(ArithmeticException e){  
            System.out.println("分母不能為0");  
            throw new ArithmeticException("分母為0拋出異常"); //拋出ArithmeticException異常  
        }  
        catch(Exception e){ //Exception是所有異常的父類(lèi)  
            System.out.println("發(fā)生了其他異常");  
        }  
        finally{ //不管是否出現(xiàn)異常,finally一定會(huì)被執(zhí)行  
            System.out.println("程序結(jié)束");  
        }   
	}
}

示例代碼:(throws)

public class Throws {
	int a=1;
	int b=0;
	public void out() throws ArithmeticException{ //聲明可能要拋出的異常,可以有多個(gè)異常,逗號(hào)隔開(kāi)
		try{ //監(jiān)聽(tīng)代碼塊
		int sum=a/b;
		System.out.println(sum);
		}
		catch(ArithmeticException e){
			System.out.println("分母不能為0");
		}
		finally{ //不管是否出現(xiàn)異常,finally一定會(huì)被執(zhí)行
			System.out.println("程序結(jié)束");
		}
	}
	public static void main(String[] args){
		Throws t=new Throws();
			t.out(); //調(diào)用方法
			throw new ArithmeticException("分母為0拋出異常"); //由調(diào)用的方法決定是否要拋出異常
			/*
			 * 第二種拋出方式
			 */
//			ArithmeticException a=new ArithmeticException("分母為0拋出異常");
//			throw a;
	}
}

看完上述內(nèi)容,你們對(duì)java異常處理的關(guān)鍵字以及使用方法大概了解了嗎?如果想了解更多相關(guān)文章內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!

當(dāng)前題目:java異常處理的關(guān)鍵字有哪些?怎么用?
文章源于:http://chinadenli.net/article0/ggppio.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站排名虛擬主機(jī)商城網(wǎng)站營(yíng)銷(xiāo)型網(wǎng)站建設(shè)標(biāo)簽優(yōu)化全網(wǎng)營(yíng)銷(xiāo)推廣

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

網(wǎng)站建設(shè)網(wǎng)站維護(hù)公司