3. 异常类

public class Example7_4 {
public static void main(String[] args) {
int n=0,m=0,t=1000;
try{
m=Integer.parseInt("8888");
n=Integer.parseInt("ab89");
t=7777;
}
catch (NumberFormatException e){
System.out.println("发生异常:"+e.getMessage());
}
System.out.println("n="+n+",m="+m+",t="+t);
try{
System.out.println("故意抛出I/O异常!");
throw new java.io.IOException("我是故意的");//故意抛出异常
//System.out.println("这个输出语句肯定没有机会执行,必须注释,否则编译");
}
catch(java.io.IOException e){
System.out.println("发生异常:"+e.getMessage());
}
}
}

由于内容不容易表达,所以我们直接拿代码举例:
我的理解:
 n=Integer.parseInt("ab89");
t=7777;//这一行代码没有被运行,因为上一句已经是一个异常代码,在上一局就已经发生异常,转向了catch;
 catch (NumberFormatException e){//用NumberFormatException类创建对象然后抛出异常

System.out.println("发生异常:"+e.getMessage());
}
 try{
System.out.println("故意抛出I/O异常!");
throw new java.io.IOException("我是故意的");//故意抛出异常 //而这句话本身是异常的,所以在这句话里,直接把这句话给了catch里的
NumberFormatException类,然后让numberFormatException类来执行抛出异常的语句



//System.out.println("这个输出语句肯定没有机会执行,必须注释,否则编译");
}
 
 
 



 
posted @ 2022-07-08 23:26  馅饼丶  阅读(134)  评论(0)    收藏  举报