The sixth and eighth week experiment summary

1. Preparation of a class, to create a one-dimensional array in its main () method, try to access array elements in the sentence, 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.

public class MyEception {

	public static void main(String[] args) {
		int a[]= {1,2,3,4,5,6,7};
		try {
			for(int i=0;i<8;i++) {
				System.out.println(a[i]);
			}
		}catch(Exception e) {
			e.getStackTrace();
			System.out.println("数字越界");
		}finally {
			System.out.println("异常处理成功");
		}

	}

}

  

 

 The results:

 

 2. Use a 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:
write a subclass DangerException Exgeption, which can create a subclass of the 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.

Experiment code:

package 危险品检查;

public class Goods{
	boolean IsDanger;
	String name;
public void setIsDanger(boolean IsDanger){
	this.IsDanger=IsDanger;
	}
 public boolean getIsDanger(){
	return IsDanger;
	}
public void setName(String name){
	    this.name=name;
	}
public String getName(){
	return name;
	}
}
	package 危险品检查;

public class DangerException extends Exception{
  String message;
 DangerException(String message){
   this.message=message;
 }
 void showMessage(){
   System.out.println(message);
 }
}
Dangerous Goods check package; 

public class Machine { 
 String name; 
  ; Goods G 
  {Machine (Goods G) 
   this.g = G; 
   name = g.getName (); 
the try { 
 IF (name.equals ( "bomb") || name .equals ( "poison") || name.equals ( "tool") || name.equals ( "poison")) { 
         System.err.println (name); 
            ! the throw new new DangerException ( "watch out for hazards found in dangerous goods! !! "); 
} 
 the else { 
   the throw new new DangerException (" No dangerous goods by checking ");!! 
 } 
} the catch (DangerException E) { 
      e.showMessage (); 
     } 
  } 
 
} 
Package dangerous goods inspection; 

Import Classes in java.util .Scanner; 

public class CheckMain { 
public static void main (String args []) { 
Scanner Scanner new new SC = (the System.in); 
OUTER: the while (to true) {
String input=sc.nextLine();
    Goods g=new Goods();
    g.setName(input);
    Machine m=new Machine(g);
if(input.equals("退出")||input.equals("结束")){
         break OUTER;
        }  
     }
   }
}

  Screenshot experiment code:

 

 

 

 

 

 

 

 Experiments run shot:

This week long study abnormal and multithreading

Leading to abnormal instruction code outage flow, so we have to consider in the design of various abnormalities occur, to ensure the normal operation of the code;

Exception handling is divided into three types:

1. no processing

2. immediate treatment

3. delay processing

Figure approach:

 

 

 Finally, exception handling exports finally:

 

 They also learned Multithreading: Multithreading is an effective means of concurrency mechanisms;

java to achieve multi-threaded code in two ways:

1. thread class inheritance;

2. Implement Runnable interface;

 

Guess you like

Origin www.cnblogs.com/hhwcg/p/11679764.html