把最后一个大括号移到System.out.println("that's my exception");} 后,变为:System.out.println("that's my exception");} }缺少默认的构造函数,把class MyException2 extends Exception{/**the second myexception*/MyException2(String s){super(s);System.out.println("that's my exception");} }改为:class MyException2 extends Exception{/**the second myexception*/MyException2(String s){super(s);System.out.println("that's my exception");} MyException2(){}}
两个类文件分开写,MyException2 .javaclass MyException2 extends Exception{/**the second myexception*/MyException2(String s){super(s);System.out.println("that's my exception");public static void main(String[] args) {TestException test=new TestException();try{int[] i=new int[2];test.add(i[0],i[1]);}catch(MyException2 e){e.printStackTrace();} }}TestException.javapublic class TestException{public int add(int a,int b) throws MyException2{int c=a+b;if (c>999) throw new MyException2("c is too big");else return c;}}是没有任何异常信息。但不知道你要什么结果了