1. 程式人生 > >Java學習---異常處理

Java學習---異常處理

 

 

 

import java.util.Scanner;

public class MyExceptionTest {
    
    public static void check(Square A) throws WrongException
    {
        if(A.getChang()<=0 && A.getKuan()<=0){
            throw new WrongException("長<=0,不合法, 寬<=0,也不合法");
        }
        
if(A.getKuan()<=0){ throw new WrongException("寬<=0,不合法"); } if(A.getChang()<=0){ throw new WrongException("長<=0,不合法"); } A.CalcuArea(); } public static void main(String[] args) { // TODO Auto-generated method stub
Square A=new Square(); Scanner in =new Scanner(System.in); System.out.println("請輸入矩形引數:\n"+"長: 寬:\n"); int Chang=in.nextInt(); A.setChang(Chang); int kuan=in.nextInt(); A.setKuan(kuan); try{ check(A); System.out.println(A.toString()); }
catch(WrongException e){ System.out.println("報錯啦\n"+e.getMessage()); e.toString(); e.printStackTrace(); }finally{ System.out.println("謝謝使用,再見!"); } } } class Square { private int Chang,Kuan; private int Area; public int getChang() { return Chang; } public String toString() { return "矩形 [長=" + Chang + ", 寬=" + Kuan + ", 面積=" + Area + "]"; } public void setChang(int chang) { Chang = chang; } public int getKuan() { return Kuan; } public void setKuan(int kuan) { Kuan = kuan; } public void CalcuArea() { Area=Chang*Kuan; } } class WrongException extends Exception { //定義一個Message String Message; //構造方法,設定Message public WrongException(String Message) { this.Message = Message; } //輸出Message public String getMessage() { return Message; } }

 

public class Test1 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int a = 100, b = 0, c;
        try{
            c = a / b;
            System.out.println("c=" + c);
        }catch(ArithmeticException e){
            System.out.println("catched!");
            System.out.println("catch ArithmeticException message:"+
            e.getMessage());
            System.out.println("catch ArithmeticException toSting():"
                    + e.toString());
            e.printStackTrace();//打印出現錯誤的地方
        }finally{
            System.out.println("finally!");
        }
    }

}

public class Test4 {

    public static void main(String[] args) {
        int a = 100, b = 2, c = 0;
        int[] x = { 10, 20, 30, 40, 50, 60, 70 };
        // 如果try寫在for迴圈外面,異常後就跳出,將不再進入迴圈
        for (int i = 0; i <= 10; i++) {
            System.out.println("c=" + c);
            try {
                c = a / b--;
                System.out.println("x[" + i + "]=" + x[i]);
            } catch (ArithmeticException e) {
                System.out.println("catched ArithmeticException message:" + e.getMessage());
                System.out.println("catched ArithmeticException toSting():" + e.toString());
                e.printStackTrace();
            } catch (ArrayIndexOutOfBoundsException e) {
                System.out.println("catched ArrayIndexOutOfBoundsException!");
            } finally {
                System.out.println("finally!");
            }
        }
    }

}

 

c=0
x[0]=10
finally!
c=50
x[1]=20
finally!
c=100
catched ArithmeticException message:/ by zero
catched ArithmeticException toSting():java.lang.ArithmeticException: / by zero
java.lang.ArithmeticException: / by zero
    at Test4.main(Test4.java:11)
finally!
c=100
x[3]=40
finally!
c=-100
x[4]=50
finally!
c=-50
x[5]=60
finally!
c=-33
x[6]=70
finally!
c=-25
catched ArrayIndexOutOfBoundsException!
finally!
c=-20
catched ArrayIndexOutOfBoundsException!
finally!
c=-16
catched ArrayIndexOutOfBoundsException!
finally!
c=-14
catched ArrayIndexOutOfBoundsException!
finally!

 

 

 

//使用 throw 語句丟擲異常、使用 throws 子句拋棄異常
public class TestThrow1 {
    static void throwProcess(Object o) throws NullPointerException{// throws NullPointerException可不寫
        if(o==null){
            throw new NullPointerException("空指標異常");//手動寫丟擲異常
        }
        //若丟擲異常則不再執行函式下面的語句
        System.out.println(o+"哈哈呵呵");
    }
    public static void main(String[] args) {
        Object p=null;
        try{
            throwProcess(p);
        }catch(NullPointerException e){
            System.out.println("捕獲:" + e);
        }
    }

}