亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Java異常處理與throws關(guān)鍵字用法分析

 更新時(shí)間:2019年01月14日 11:26:02   作者:貝貝愛(ài)豆豆  
這篇文章主要介紹了Java異常處理與throws關(guān)鍵字用法,結(jié)合實(shí)例形式分析了java常見(jiàn)的異常、錯(cuò)誤處理及throws關(guān)鍵字相關(guān)使用技巧、注意事項(xiàng),需要的朋友可以參考下

本文實(shí)例講述了Java異常處理與throws關(guān)鍵字用法。分享給大家供大家參考,具體如下:

Java異常處理

認(rèn)識(shí)異常:

1.異常是導(dǎo)致程序中斷運(yùn)行的一種指令流,如果不對(duì)異常進(jìn)行正確處理,則可能導(dǎo)致程序的中斷執(zhí)行,造成不必要的損失。

2.異常范例

空指針異常

Exc e=null;
System.out.println(e.i);

除0異常

int a=10;
int b=0;
System.out.println(a/b);

3.處理異常

異常格式:

try{
異常語(yǔ)句;
}
catch(Exception e){
}
finally{
   一定會(huì)執(zhí)行的代碼;
}

int a=10;
int b=0;
try {
   System.out.println(a/b);
}
catch (ArithmeticException e){
   System.out.println(e);
}

int temp=0;
Exc e=null;
try {
  temp=e.a/e.b;
  System.out.println(temp);
}
catch (NullPointerException e1){
  System.out.println("空指針異常"+e1);
}
catch (ArithmeticException e1){
  System.out.println("算數(shù)異常"+e1);
}
finally {
  System.out.println("程序退出");
}

常見(jiàn)異常

1.數(shù)組越界異常:ArrayIndexOutOfBoundsException

2.數(shù)字格式化異常:NumberFormatException

3.算數(shù)異常:ArithmeticException

4.空指針異常:NullPointerException

throws關(guān)鍵字

1.在定義一個(gè)方法的時(shí)候可以使用throws關(guān)鍵字聲明,使用throws聲明的方法表示此方法不處理異常,拋給方法的調(diào)用者處理。

2.格式:

public void tell()throws Exception{}

例子:

public static void main(String [] args){
    try {
      tell(10,0);
    }
    catch (Exception e){
      System.out.println(e);
    }
}
public static void tell(int i,int j)throws ArithmeticException{
    int temp=0;
    temp=i/j;
    System.out.println(temp);
}

還可以:

public static void main(String [] args)throws Exception{
    tell(10,0);
}
public static void tell(int i,int j)throws ArithmeticException{
    int temp=0;
    temp=i/j;
    System.out.println(temp);
}

此時(shí),最后拋給JVM進(jìn)行處理。

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

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

相關(guān)文章

最新評(píng)論