Eighth week experiment

Experiments six Java exception

Purpose

Understand the basic concepts abnormal;
grasp exception handling and familiar with common method to capture the exception.

Experimental requirements

Practice catch the exception, unusual statement, the method throws an exception, familiar with the try and catch clauses.
Master the method custom exception class.

Content Experiments

Write a class, create a one-dimensional array in its main () method, try to access the array elements in the words, to produce ArrayIndexOutOfBoundsException exception. In the catch clause catches this exception object, and prints "array bounds" information, plus a finally clause and print a message to prove that there really has been implemented.
Use custom exception class
equipment inspection station of dangerous goods, if found dangerous goods will be issued a warning. Programming Analog Devices discovery of dangerous goods.

Technical solutions:

DangerException Exgeption write a subclass of the subclass can create an exception object, call the exception object toShow () method outputs "dangerous goods." Machine to write a class that way checkBag (Goods goods) when the goods are found parameter of dangerous goods (goods of isDanger property is true) DangerException will throw an exception.
In the main program the main class () try part of the method of try-catch statement let instance of the Machine class checkBag (Goods goods) method call, if found dangerous goods on the part of the handling of dangerous goods in the catch try-catch statement.

Use custom exception class:

public class Try {
    public static void main(String args[]) {

        try {
            int score[] = {0, 1, 2};
            System.out.println("输出第四个数组元素:" + score[3]);
        }
               catch(ArrayIndexOutOfBoundsException e){
                System.out.println("数组越界:"+e);
        }
        finally {
            System.out.println("over");
        }

    }
}

import java.util.Scanner;
import java.util.ArrayList;
public class Danger {
    public static void main(String[] args) {
        ArrayList<String> list=new ArrayList<String>();
        list.add("爆炸物品");
        list.add("管制刀具");
        list.add("打火机");
        list.add("毒品");         
        System.out.println("请输入需要检查的物品:");
        Scanner n=new Scanner(System.in);
        String s=n.next();         
        Goods goods=new Goods(s);
        Mechine mechine=new Mechine();      
        try {
            mechine.checkBag(goods);       
        } catch (DangerException e) {
            if(list.contains(s)) {
                e.toShow();           
                System.out.println(goods.getName() + ":危险");   
            }
            else{
                System.out.println(goods.getName() + ":安全");
            }
        }
    }
}

public class Goods {
    private String name;
    boolean isDanger=true;
    public Goods(String name) {    
        this.name=name;           
    }
    public Goods() {
    }                     
    public String getName() {
        return name;           
    }
}

public class Mechine {
    public void checkBag(Goods goods)throws DangerException{   
        if(goods.isDanger){                      
            DangerException danger=new DangerException();
            throw danger;             
        }
        else{
            System.out.println(goods.getName()+ "安全");
        }
    }
}

class DangerException extends Exception {
    public void toShow() {
        System.out.println("危险物品");  
     }
}

Guess you like

Origin www.cnblogs.com/xia-unun/p/11702729.html