当前位置: 技术问答>java相关
请大家帮我解释一下这个异常错在哪?
来源: 互联网 发布时间:2015-11-14
本文导语: import java.io.IOException; public class ExceptionTest{ public static void main (String[]args) { try { methodA();} catch (IOException e) { System.out.println("Caught IOException"); } catch (Exception e) { System.out.println("Caught Exception"); } } publ...
import java.io.IOException;
public class ExceptionTest{
public static void main (String[]args) {
try { methodA();}
catch (IOException e) { System.out.println("Caught IOException"); }
catch (Exception e) { System.out.println("Caught Exception"); }
}
public static void methodA () { throw new IOException ();
}
}
错误如下: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"); }
^
ExceptionTest.java:8: Exception java.io.IOException must be caught, or it must b
e declared in the throws clause of this method.
public static void methodA () { throw new IOException ();
public class ExceptionTest{
public static void main (String[]args) {
try { methodA();}
catch (IOException e) { System.out.println("Caught IOException"); }
catch (Exception e) { System.out.println("Caught Exception"); }
}
public static void methodA () { throw new IOException ();
}
}
错误如下: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"); }
^
ExceptionTest.java:8: Exception java.io.IOException must be caught, or it must b
e declared in the throws clause of this method.
public static void methodA () { throw new IOException ();
|
public static void methodA () { throw new IOException ();
}
该成
public static void methodA () {
try
{
throw new IOException ();
}
catch(Exception e){}
}
}
该成
public static void methodA () {
try
{
throw new IOException ();
}
catch(Exception e){}
}
|
import java.io.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();
}
}
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();
}
}
|
改成这样试试
public static void methodA () throws IOException{
public static void methodA () throws IOException{
您可能感兴趣的文章:
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。