上面的源码没注意格式,重新发一遍。 class MyException extends Exception { private int id; public MyException(String message, int id){ super(message); this.id = id; } public int getId(){ return id; } }public class TestMyException{ public static void regist(int num) throws MyException{ if(num<0){ throw new MyException("num is errorous",3); } System.out.println("The number of registion is :"+num); } public static void main(String[] args) throws Exception{ try{ regist(-1); }catch(MyException e){ System.out.println("The code of error is:"+e.getId()); //Thread.sleep(1000); e.printStackTrace(); } System.out.println("Operation is over"); } }
我的问题就是为什么是不固定的,难道异常又用了一个线程?还是有其他原因?否则感觉应该是按照顺序执行,先打印"The code of error is:3”,然后打印异常信息,这以后还得打印“Operation is over",但顺序总是在变化。为啥?
class MyException extends Exception {
private int id;
public MyException(String message, int id){
super(message);
this.id = id;
}
public int getId(){
return id;
}
}public class TestMyException{
public static void regist(int num) throws MyException{
if(num<0){
throw new MyException("num is errorous",3);
}
System.out.println("The number of registion is :"+num);
}
public static void main(String[] args) throws Exception{
try{
regist(-1);
}catch(MyException e){
System.out.println("The code of error is:"+e.getId());
//Thread.sleep(1000);
e.printStackTrace();
}
System.out.println("Operation is over");
}
}
流不一样。