1 用 try-catch的方法。。
package why;
import java.util.*;
public class iiii {
public static void main(String args[])
{ int n=0,m=0,t=0;
try
{ t=9999;
m=Integer.parseInt("8888");
n=Integer.parseInt("13a");
System.out.println("我没有机会输出");
}
catch(Exception e)
{ System.out.println("eror");
System.out.println(m);
}
}
}
2 自定义异常类
要注意如果错误就抛出。然后在catch里处理
package why;
import java.util.*;
class Myexception extends Exception
{ String message;
Myexception (int n)
{ message=n+"不是正数";
}
public String getMessage()
{ return message;
}
}
public class lovestick {
public static void main(String args[])
{ try
{ int m=2;
int s=-1;
if(s<0)
{
Myexception xx=new Myexception(s);
throw(xx);
}
}
catch(Myexception xx)
{ System.out.println(xx.getMessage());
}
}
}