源码1:
package shiyan6;
import java.util.Scanner;
public class Shuzuyichang {
public static void main(String[] args){
System.out.println("程序开始运行");
int[] i=new int[3];
@SuppressWarnings("resource")
Scanner k=new Scanner(System.in);
int j=k.nextInt();
try {
int a=i[j];
a=a+1;
}catch (ArrayIndexOutOfBoundsException ae){
System.err.println("数组越界异常:"+ae);
}
finally {
System.out.println("程序运行结束");
}
}
}
截图1:
源码2:
package shiyan6;
@SuppressWarnings("serial")
public class DangerException extends Exception{
String weixianpin;
public DangerException(){
this.weixianpin="weixianpin";
}
public void toShow(){
System.err.println("警告!!!");
}
}
package shiyan6;
public class Goods {
private String name;
boolean isDanger=true;
public Goods(String name){
this.name=name;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
package shiyan6;
public class Machine {
public void checkBag(Goods goods) throws DangerException{
if(goods.isDanger){
DangerException de=new DangerException();
throw de;
}
else {
System.out.println(goods.getName()+"不是危险物品!");
}
}
}
package shiyan6;
import java.util.Scanner;
import java.util.ArrayList;
public class Test {
public static void main(String[] args) {
ArrayList<String> list=new ArrayList<String>();{{
list.add("管制刀具");
list.add("汽油");
list.add("枪");
list.add("易爆物品");
list.add("易燃物品");
}};
System.out.println("您的物品需要检测方予通过");
@SuppressWarnings("resource")
Scanner n=new Scanner(System.in);
String wupin=n.next();
Goods goods=new Goods(wupin);
Machine m=new Machine();
System.out.println("物品检测中......");
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
}
try {
m.checkBag(goods);
} catch (DangerException ae) {
if(list.contains(wupin)) {
ae.toShow();
System.err.println(goods.getName() + "-->>属危险物品不予通过");
}
else{
System.out.println(goods.getName() + ":检查通过");
}
}
}
}
截图2: