Eight-week course and test report summary

Experiments six Java abnormal

  • Purpose

          Exception: An instruction causes the program flow of the terminal.

          And catching an exception processing method: There are two exception processing method, processing and delay processing immediately, the processing is immediately try {} catch {} to be processed;

Delayed treatment with throws keyword, in this unusual method call time to resolve exceptions.

  • 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

Experiment code:

package twst;

public class demo {
    public static void main(String args[]) {
        try {
            int temp;
            String[] str={"1","5"};
            String st1=str[1];
            String st2=str[2];
            int i=Integer.parseInt(st1);
            int j=Integer.parseInt(st2);
            temp=i/j;
            System.out.println(temp);
        }catch(ArrayIndexOutOfBoundsException e) {
            System.out.println ( "array bounds:" + E);
        }
        finally {
            System.out.println ( "print a message to prove that there really have been implemented" );
        }
    }
}

 

 

 

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.

 

 

Question: This question is a long time to write, can not write, can not write or read someone else's.

Guess you like

Origin www.cnblogs.com/shigedidi/p/11698992.html