Summary and eighth week experiment six

Java exception sixth experiment
experimental purposes
understand the basic concepts abnormal;
grasp familiar with common exception handling and capture abnormal way.
Experimental requirements
to practice catch the exception, unusual statement, the method throws an exception, and familiar with the use try and catch clauses.
Master the method custom exception class.
Experimental content
to 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.
Experiment code
package checkdanger;

public class Dangerous {
public static void main(String[] args) {
int ch[]= {0,1,2,3,4};
try {
System.out.print(ch[6]);
}catch(ArrayIndexOutOfBoundsException a){
System.out.println("数组越界:"+a);
}finally {
System.out.println("执行程序");
}
}

}

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:
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 ood;

public class DangerException extends Exception {
String infor;
public DangerException(String s) {
infor=s+"危险物品";
}
public void toshow() {
System.out.println(infor);
}

}

package ood;

class Goods{
String name;
boolean isDanger;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public boolean isDanger() {
return isDanger;
}
public void setisDanger(boolean b) {
isDanger = b;
}

package ood;

public void checkBag(Goods goods) throws DangerException {
if(goods.isDanger()) {
throw new DangerException(goods.getName());
}
else {
System.out.println(goods.getName()+"非危险物品");
}
}
}

package ood;
public class test {
public static void main(String[] args) {
Machine m=new Machine();
Goods goods=new Goods();
String str[]= {"枪支","刀子","化妆品","手机","火药","汽油"};
for(int i=0;i<str.length;i++) {
goods.setName(str[i]);
if(goods.getName().equals("枪支")||goods.getName().equals("刀子")||goods.getName().equals("火药")||goods.getName().equals("汽油")) {
goods.setisDanger(true);
}
else {
goods.setisDanger(false);
}
try {
m.checkBag(goods);
}
catch(DangerException e){
e.toshow();
}
}

}

}

}

Run shot

Study concluded:
NO.1 inherit the Thread on
when you start multiple threads, you can not directly call run () method, you must first enable the start () method
NO.2 in dealing exception to throw How to throw an exception still a little confused
NO .3 not proficient try () catch () finally ( ) and nested inheritance to achieve code can only say JAVA long way to go

Guess you like

Origin www.cnblogs.com/Vennien/p/11701516.html