【发布时间】:2016-02-20 11:17:22
【问题描述】:
在 try 块中捕获不抛出异常子类的方法的异常,编译失败。当我捕捉到异常时,它起作用了。它是如何工作的??
下面这段代码不能编译!!
class Test
{
public static void main(String[] args) {
try {
myMethod();
} catch(MyException ex) {//Does not compile
}
}
public static void myMethod() {}
}
类 MyException 扩展异常 {}
但是当异常被捕获时,编译器不会抱怨。
class test
{
public static void main(String[] args)
{
try{
myMethod();
} catch(Exception ex) {//how does this works ??
}
}
public static void myMethod() {}
}
【问题讨论】: