[英]Checked and Unchecked Custom Exception
I am confused about the behaviour of the CustomExceptions
in below program.If Line2
is commented and Line1
is not then program works well,but if Line1
is commented and Line2
is not then compile time error comes as "Unreachable catch block for CustomChecked.This exception is never thrown from the try statement body" for Line3.Please help me why this comple time exception comes only for the unChecked Exception?. 我感到困惑的行为
CustomExceptions
下面program.If Line2
的评论和Line1
不是那么程序运作良好,但如果Line1
的评论和Line2
不那么编译时错误当属“为CustomChecked.This例外无法到达的catch块不会从Line3的try语句主体中抛出”。请帮助我为什么此comple time异常仅用于unChecked Exception ?。
try
{
if(true)
{
throw new CustomChecked("Checked Exception");// Line1
// throw new CustomUnChecked("Un-Checked Exception");// Line2
}
}
catch(CustomChecked ex) //Line3
{
System.out.println(ex.getMessage());
}
catch(CustomUnChecked ex)
{
System.out.println(ex.getMessage());
}
Exceptions : 例外情况:
class CustomChecked extends Exception
{
public CustomChecked(String msg) {
super(msg);
}
}
class CustomUnChecked extends RuntimeException
{
public CustomUnChecked(String msg) {
super(msg);
}
}
CustomUnChecked is a RuntimeException
which is unchecked
meaning that the complier doesn't check who can/do throw
that exception
. CustomUnChecked是一个
RuntimeException
,它unchecked
意味着编译器不检查谁可以/可以throw
该exception
。 Compiler assumes that every object/method/code block can throw
any type of RuntimeException
and so that you can always check for this type of exceptions in your check
statements. 编译器假定每个对象/方法/代码块都可以
throw
任何类型的RuntimeException
,因此您始终可以在check
语句中检查这种类型的异常。
On the other hand CustomChecked is checked, meaning that the compiler checks for all methods and code blocks that can throw it. 另一方面,对CustomChecked进行了检查,这意味着编译器会检查所有可能引发该错误的方法和代码块。 So you can only
catch
the checked exception if you know (at a complie time) that you invoke a method/code that throws the exception inside the try
block. 因此,只有在知道(在编译时)知道调用了将异常抛出到
try
块内的方法/代码时,您才能catch
已检查的异常。
So to sum up - your compilator informs you, that the catch
you have in line 3 is not needed (and so should be removed), because there is no way that somebody will throw
CustomCheckedException inside the try statement. 综上所述-编译器会通知您,不需要(第3行中的)
catch
,因此应该删除它,因为没有人会在try语句内throw
CustomCheckedException 。 As CustomUnCheckedException is not checked
by compiler
(it is runtime, assumed to be thrown unexpected anywere), the check
statement for it can stay. 由于
compiler
未checked
CustomUnCheckedException (它是运行时,假定会意外抛出),因此可以保留它的check
语句。
The part of the Java Spec that is applicable here is 11.2.3 . Java Spec中适用的部分是11.2.3 。 It reads
它读
It is a compile-time error if a catch clause can catch checked exception class E1 and it is not the case that the try block corresponding to the catch clause can throw a checked exception class that is a subclass or superclass of E1, unless E1 is Exception or a superclass of Exception.
如果catch子句可以捕获经过检查的异常类E1,则是编译时错误,并且与catch子句相对应的try块不能抛出作为E1的子类或超类的经过检查的异常类,除非E1是Exception或Exception的超类。
Notice that this clause is restricted to 'checked' exceptions. 注意,此子句仅限于“检查”异常。 Unchecked exceptions, by definition of being 'unchecked' do not have these checks applied to them.
根据“未检查”的定义,未检查的异常不会应用这些检查。 Hence the difference in behavior that you are seeing.
因此,您看到的行为有所不同。
The compiler does not check if you catch RuntimeExceptions, but it does check that you catch Exceptions. 编译器不会检查您是否捕获了RuntimeException,但会检查您是否捕获了异常。 Thats why you get an error if you try to catch a non-existant checked exception (if its not in the throws clause, it shouldnt exist, since its a checked exception), and no error if you are catching a RuntimeException (which can happen even through you didnt put it in the throws clause, a NullPointerException for example).
这就是为什么如果尝试捕获不存在的已检查异常(如果该异常不在throws子句中,则它不应该存在,因为它是一个已检查的异常)而导致错误,并且如果您捕获了RuntimeException(可能发生,则不会出错)即使您没有将它放在throws子句中,例如NullPointerException)。
So, in short, CheckedExceptions MUST be declared in the throws clause and MUST be catched. 简而言之,CheckedExceptions必须在throws子句中声明,并且必须被捕获。 Unchecked exceptions CAN be catched, but the compiler has no way of knowing the if code throws an unchecked exception, so it wont give you an error if you check on it or not.
可以捕获未经检查的异常,但是编译器无法知道if代码是否抛出未经检查的异常,因此,无论是否进行检查,它都不会给您带来错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.