Gur*_*nda 2 java exception-handling
public class simple {
public static void main(String[] args) {
try {
System.out.print("hello ");
throwit();
} catch (Exception re) {
System.out.print("caught ");
}
}
public static void throwit(){ // line number 11
throw new Exception(); // line number 12
}
}
Run Code Online (Sandbox Code Playgroud)
为什么它会给我一个第12行的编译错误.如果我对行号11使用throws Exception,那么它工作正常.如果我抛出Exception的子类(在第12行),那么它可以正常工作......为什么呢?...
我想知道背面发生了什么(编译器如何显示错误)?
Eri*_*ica 10
你有一个方法抛出一个已检查的异常,但它的方法签名并没有指定它能够做到这一点.必须在方法签名中声明所有已检查的异常,并通过try/catch块或重新抛出显式处理; 这就是被检查异常的定义.:)
这一行:
public static void throwit()
Run Code Online (Sandbox Code Playgroud)
应该
public static void throwit() throws Exception
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1381 次 |
最近记录: |