Eight-week course Summary - Experimental Report VI (Java abnormal)

Understand the basic concepts abnormal;

Master and exception handling are familiar with common method of catching exceptions

Experimental requirements

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

Methods custom exception class master

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.

Using custom exception classes

package Domain;

public class ExceptionTest {

    public static void main(String[] args)throws Exception {
        int[]arr = new int[10];
        try{
            int a  = arr[10];
            System.out.println(a);
        }catch(ArrayIndexOutOfBoundsException ex){
            throw new Exception("数组越界",ex);
        }finally{
            System.out.println("验证Finally的运行");
        }
    }

}

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.

package Danger;

public class DangerException extends Exception{
    String imformation;
    
    DangerException(String imformation){
        this.imformation=imformation;
    
    }
    void toShow(){
        
            System.out.println(imformation);
   }
}
package Danger;

public class Machine {
      String name;
        Goods g;

        public boolean isDanger(String name) {
            String score[] = {"炸弹","毒药","刀具","枪支"};
            boolean flag =false;
            for(int i=0;i<score.length;i++) {
                if(name.equals(score[i])) {
                flag = true;
                break;
                }
            }
            return flag;
             
        }

        void checkBag(Goods g){
            this.g=g;
            name=g.getName();
            try{
                if(isDanger(name)){
                    System.out.print(name);
                    throw new DangerException("是危险品!!!"+"\n");
                }
                else{
                    System.out.print(name);
                    throw new DangerException("不是危险品!"+"\n");
                }
            }catch(DangerException e){
                  e.toShow();
            }
        }
}
package Danger;

public class Goods{
    
    String name;
    
    public void setName(String name){
    this.name=name;
    }
    public String getName(){
    return name;
    }
}
package Danger;
import java.util.Scanner;
public class Test {
     public static void main(String[] args) {
            
            while(true) {
                Scanner sc=new Scanner(System.in);
                
                System.out.println("请输入物品:");
                String input=sc.nextLine();
                Goods g=new Goods();
                g.setName(input);
                Machine m=new Machine();
                m.checkBag(g);
            
            }
        }

}

Lessons Learned

Understanding of the processes and threads, multi-threading refers to a process in the implementation process, can produce several smaller program units (ie thread), these threads can exist simultaneously, running at the same time ,, but may contain more than one process Meanwhile thread of execution.

Thread class inheritance

When you start multiple threads, you must () method to start by start, but can not call run () method to achieve if a class by inheriting Thread directly, you can only call once start () method, if called multiple times will throw. " IlleglThreadStateException "exception.

Implement Runnable

Abstract method only provides a Runnable interface in run ()

Guess you like

Origin www.cnblogs.com/buxiu888/p/11701136.html