public static void methodA () { throw new IOException (); } 该成 public static void methodA () { try { throw new IOException (); } catch(Exception e){} }
无论怎么改后,结果还都是:ExceptionTest.java:5: Exception java.io.IOException is never thrown in the body of the corresponding try statement. catch (IOException e) { System.out.println("Caught IOException"); } ^ 实在不明白怎么回事?
}
该成
public static void methodA () {
try
{
throw new IOException ();
}
catch(Exception e){}
}
of the corresponding try statement.
catch (IOException e) { System.out.println("Caught IOException"); }
^
实在不明白怎么回事?
catch (IOException e) { System.out.println("Caught IOException"); }
该成
try { methodA();
throw new IOException();
}
catch (IOException e) { System.out.println("Caught IOException"); }
public class ExceptionTest{
public static void main (String[]args) {
try {
ExceptionTest tt = new ExceptionTest();
tt.methodA();
}
catch (IOException e) { System.out.println("Caught IOException"); }
catch (Exception e) { System.out.println("Caught Exception"); }
}
public void methodA() throws IOException{ throw new IOException();
}
}
这样好了,可不明白为什么 throw 一定要在调用方法后!在方法中 throw,调用后catch不就行了吗?
跟调用顺序有什么关系吗?
变成public static void methodA () throws IOException
也就是说,你必须有可能产生IO异常