Java中的異常處理機制的簡單原理和應用~~
JAVA程序違反了JAVA的語義規則時,JAVA虛擬機就會將發生的錯誤表示為一個異常。違反語義規則包括2種情況。一種是JAVA類庫內置的語義檢查。例如數組下標越界,會引發IndexOutOfBoundsException;訪問null的對象時會引發 NullPointerException。另一種情況就是JAVA允許程序員擴展這種語義檢查,程序員可以創建自己的異常,并自由選擇在何時用 throw關鍵字引發異常。所有的異常都是java.lang.Thowable的子類。
eg:第一種
class Test{
public int devide(int x ,int y) throws Exception{ //
int result = x/y;
return result;
}
}
class TestException{
public static void main(String[] args){
try{
new Test().devide(3,0);
}catch(Exception e){
System.out.println(e.getMessage());//雖然Exception除了Constructor之外沒有其它的函數,
//但它繼承了Throwable類.
}
System.out.println("Program running here");
}
}
當編寫Test class不知道它是否拋出異常時,可以在devide methods方法后添加 throws Exception 就可以..而在TestException中必須處理拋出的異常.
eg:第二種
如果自己定義異常類,必須繼承Exception,也就是它的子類.
class Test{
public int devide(int x ,int y) throws Exception{ //
if(y < 0)
throw DevideMyMinusException("devide is" + y);
int result = x/y;
return result;
}
}
class DevideMyMinusException extends Exception{
public DevideMyMinusException (String msg){
super(msg); //調用父類(Exception)的Construtor,主函數getMessage()輸出自定義的異常:"devide is..."
}
}
eg.第三種
可以拋出多個異常類.
class Test{
public int devide(int x ,int y) throws ArithmeticException,DevideMyMinusException{ //
if(y < 0)
throw new DevideMyMinusException("devide is" + y);
int result = x/y;
return result;
}
}
class DevideMyMinusException extends Exception{
public DevideMyMinusException (String msg){
super(msg);
}
}
class TestException{
public static void main(String[] args){
try{
new Test().devide(3,-1);
}catch(ArithmeticException e){
System.out.println("program running into ArithmeticException ");
e.printStackTrace();
}catch(DevideMyMinusException e){
System.out.println("program running into DevideMyMinusException");
e.printStackTrace();
}catch(Exception e){
System.out.println(e.getMessage)
)finally{
System.out.println("finally running");//不管程序發生異常沒有,finally代碼塊都要執行.
}
System.out.println("Program running here");
}
}
1.程序根據異常類型的匹配.自動進入相應的catch語句.Exception應放在其它異常語句后,因為他們都繼承Exception ,其它異常要放在后面,就沒有什么意義了.
2.try {}里有一個return語句,那么緊跟在這個try后的finally {}里的code會不會被執行,什么時候被執行,在return前還是后? 會執行,而且在return 前執行.
3.什么時候finally里的代碼不會執行呢? 當出現System.exit(0);時,它不會執行,程序會退出.
eg:第一種
class Test{
public int devide(int x ,int y) throws Exception{ //
int result = x/y;
return result;
}
}
class TestException{
public static void main(String[] args){
try{
new Test().devide(3,0);
}catch(Exception e){
System.out.println(e.getMessage());//雖然Exception除了Constructor之外沒有其它的函數,
//但它繼承了Throwable類.
}
System.out.println("Program running here");
}
}
當編寫Test class不知道它是否拋出異常時,可以在devide methods方法后添加 throws Exception 就可以..而在TestException中必須處理拋出的異常.
eg:第二種
如果自己定義異常類,必須繼承Exception,也就是它的子類.
class Test{
public int devide(int x ,int y) throws Exception{ //
if(y < 0)
throw DevideMyMinusException("devide is" + y);
int result = x/y;
return result;
}
}
class DevideMyMinusException extends Exception{
public DevideMyMinusException (String msg){
super(msg); //調用父類(Exception)的Construtor,主函數getMessage()輸出自定義的異常:"devide is..."
}
}
eg.第三種
可以拋出多個異常類.
class Test{
public int devide(int x ,int y) throws ArithmeticException,DevideMyMinusException{ //
if(y < 0)
throw new DevideMyMinusException("devide is" + y);
int result = x/y;
return result;
}
}
class DevideMyMinusException extends Exception{
public DevideMyMinusException (String msg){
super(msg);
}
}
class TestException{
public static void main(String[] args){
try{
new Test().devide(3,-1);
}catch(ArithmeticException e){
System.out.println("program running into ArithmeticException ");
e.printStackTrace();
}catch(DevideMyMinusException e){
System.out.println("program running into DevideMyMinusException");
e.printStackTrace();
}catch(Exception e){
System.out.println(e.getMessage)
)finally{
System.out.println("finally running");//不管程序發生異常沒有,finally代碼塊都要執行.
}
System.out.println("Program running here");
}
}
1.程序根據異常類型的匹配.自動進入相應的catch語句.Exception應放在其它異常語句后,因為他們都繼承Exception ,其它異常要放在后面,就沒有什么意義了.
2.try {}里有一個return語句,那么緊跟在這個try后的finally {}里的code會不會被執行,什么時候被執行,在return前還是后? 會執行,而且在return 前執行.
3.什么時候finally里的代碼不會執行呢? 當出現System.exit(0);時,它不會執行,程序會退出.