📄 exceptiondemo14.java
字号:
class 危险物品{
String 名称;
public 危险物品(String 名称){
this.名称 = 名称;
}
public String toString(){
return "危险物品:"+名称;
}
}
class 炸药{
public String toString(){
return "炸药!!!";
}
}
class 警告 extends RuntimeException{
public 警告(String s){
super(s);
}
}
class 危险 extends Exception{
public 危险(String s){
super(s);
}
}
class 食品{
String 名称;
public 食品(String 名称){
this.名称 = 名称;
}
public String toString(){
return "食品:"+名称;
}
}
public class ExceptionDemo14{
public static void 检查物品(Object a) throws 危险{
if(a instanceof 危险物品) throw new 警告("有危险物品");
if(a instanceof 炸药) throw new 危险("有非常危险物品!!");
System.out.println("检查结果为正常");
}
public static void main(String arg[]){
Object 行李[] = {new 食品("饼干"), new 食品("可乐"), new 炸药(), new 危险物品("小刀"),new 食品("茶叶")};
for(int i =0; i< 行李.length; i++){
try{
检查物品(行李[i]);
}catch(危险 e){
System.out.println("发现危险物品");
}finally{
System.out.println("物品是:"+行李[i] +"\n");
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -