一、捕獲異常
異常是導(dǎo)致程序中斷運(yùn)行的一種指令流,如果不對異常進(jìn)行正確處理坷备,則可能導(dǎo)致程序的中斷執(zhí)行假栓,造成不必要的損失猾漫。
1点晴、異常范例
【示例-1】空指針異常
代碼
class Exc{
int i = 10;
}
public class Test49 {
public static void main(String[] args) {
Exc c = new Exc();
System.out.println(c.i);
Exc b = null;
System.out.println(b.i); //此部分會(huì)報(bào)空指針異常
}
}
結(jié)果:
10
Exception in thread "main" java.lang.NullPointerException
at cn.sec.ch01.Test49.main(Test49.java:16)
【示例-2】算數(shù)異常
代碼
public class Test49 {
public static void main(String[] args) {
int a = 10;
int b = 0;
int temp = a/b;
System.out.println(temp);
}
}
異常結(jié)果:
Exception in thread "main" java.lang.ArithmeticException: / by zero
at cn.sec.ch01.Test49.main(Test49.java:16)
2、處理異常
異常格式:
try{
異常語句
}catch(Exception e){
}finally{
一定會(huì)執(zhí)行的代碼
}
示例
代碼
public class Test49 {
public static void main(String[] args) {
int a = 10;
int b = 0;
int temp = 0;
try {
temp = a/b;
} catch (Exception e) {
System.out.println(e);
}
System.out.println(temp);
}
}
結(jié)果:
java.lang.ArithmeticException: / by zero
0
二悯周、常見異常
- 數(shù)組越界異常:
ArrayIndexOutOfBoundsException
- 數(shù)字格式化異常:
NumberFormatException
- 算數(shù)異常:
ArithmeticException
- 空指針異常:
NullPointerException
代碼一
class Exam{
int a = 10;
int b = 0;
}
public class Test50 {
public static void main(String[] args) {
Exam e = null; //創(chuàng)建對象
e = new Exam();//實(shí)例化
int temp = 0;
try {
temp = e.a/e.b;
System.out.println(temp);
} catch (NullPointerException e2) {
System.out.println("空指針異常:"+e2);
} catch (ArithmeticException e2) {
System.out.println("算數(shù)異常:"+e2);
}finally {
System.out.println("程序退出");
}
}
}
結(jié)果:
算數(shù)異常:java.lang.ArithmeticException: / by zero
程序退出
代碼二
class Exam{
int a = 10;
int b = 10;
}
public class Test50 {
public static void main(String[] args) {
Exam e = null; //創(chuàng)建對象
// e = new Exam();//實(shí)例化
int temp = 0;
try {
temp = e.a/e.b;
System.out.println(temp);
} catch (NullPointerException e2) {
System.out.println("空指針異常:"+e2);
} catch (ArithmeticException e2) {
System.out.println("算數(shù)異常:"+e2);
}finally {
System.out.println("程序退出");
}
}
}
結(jié)果:
空指針異常:java.lang.NullPointerException
程序退出
代碼三
class Exam{
int a = 10;
int b = 10;
}
public class Test50 {
public static void main(String[] args) {
Exam e = null; //創(chuàng)建對象
e = new Exam();//實(shí)例化
int temp = 0;
try {
temp = e.a/e.b;
System.out.println(temp);
} catch (NullPointerException e2) {
System.out.println("空指針異常:"+e2);
} catch (ArithmeticException e2) {
System.out.println("算數(shù)異常:"+e2);
}finally {
System.out.println("程序退出");
}
}
}
結(jié)果:
1
程序退出
三粒督、throws關(guān)鍵字
- 在定義一個(gè)方法的時(shí)候可以使用throws關(guān)鍵字聲明,使用throws聲明的方法表示此方法不處理異常禽翼,拋給方法的調(diào)用者處理屠橄。如果是主方法拋出異常,就是JVM進(jìn)行處理
- 格式:
public void tell() throws Exception{}
示例
代碼
public class Test51 {
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);
}
}
結(jié)果:
java.lang.ArithmeticException: / by zero
四闰挡、throw關(guān)鍵字
throw關(guān)鍵字拋出一個(gè)異常锐墙,拋出的時(shí)候直接拋出異常類的實(shí)例化對象即可。
示例
代碼
public class Test52 {
public static void main(String[] args) {
try {
throw new Exception("實(shí)例化異常對象");
} catch (Exception e) {
System.out.println(e);
}
}
}
結(jié)果:
java.lang.Exception: 實(shí)例化異常對象
五长酗、自定義異常
自定義異常直接集成Exception就可以完成自定義異常類溪北。
示例
代碼
package cn.sec.ch01;
class MyException extends Exception{
public MyException (String msg) {
super(msg);
}
}
public class Test53 {
public static void main(String[] args) {
try {
throw new MyException("自定義異常");
} catch (MyException e) {
System.out.println(e);
}
}
}
結(jié)果:
cn.sec.ch01.MyException: 自定義異常