Java 在相应的 try 语句的主体中永远不会抛出异常
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22613423/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Exception is never thrown in body of corresponding try statement
提问by user3407967
I have a problem with exception handling in Java, here's my code. I got compiler error when I try to run this line: throw new MojException("Bledne dane");
. The error is:
我在 Java 中处理异常时遇到问题,这是我的代码。我得到了编译器错误,当我尝试运行这行:throw new MojException("Bledne dane");
。错误是:
exception MojException is never thrown in body of corresponding try statement
异常 MojException 永远不会在相应的 try 语句的主体中抛出
Here is the code:
这是代码:
public class Test {
public static void main(String[] args) throws MojException {
// TODO Auto-generated method stub
for(int i=1;i<args.length;i++){
try{
Integer.parseInt(args[i-1]);
}
catch(MojException e){
throw new MojException("Bledne dane");
}
try{
WierszTrojkataPascala a = new WierszTrojkataPascala(Integer.parseInt(args[0]));
System.out.println(args[i]+" : "+a.wspolczynnik(Integer.parseInt(args[i])));
}
catch(MojException e){
throw new MojException(args[i]+" "+e.getMessage());
}
}
}
}
And here is a code of MojException:
这是 MojException 的代码:
public class MojException extends Exception{
MojException(String s){
super(s);
}
}
Can anyone help me with this?
谁能帮我这个?
采纳答案by Christian
A catch-block in a try statement needs to catch exactlythe exception that the code inside the try {}
-block canthrow (or a super class of that).
try 语句中的 catch-block 需要准确地捕获try {}
-block内的代码可以抛出的异常(或它的超类)。
try {
//do something that throws ExceptionA, e.g.
throw new ExceptionA("I am Exception Alpha!");
}
catch(ExceptionA e) {
//do something to handle the exception, e.g.
System.out.println("Message: " + e.getMessage());
}
What you are trying to do is this:
你想要做的是:
try {
throw new ExceptionB("I am Exception Bravo!");
}
catch(ExceptionA e) {
System.out.println("Message: " + e.getMessage());
}
This will lead to an compiler error, because your java knows that you are trying to catch an exception that will NEVER EVER EVER occur. Thus you would get: exception ExceptionA is never thrown in body of corresponding try statement
.
这将导致编译器错误,因为您的 Java 知道您正在尝试捕获永远不会发生的异常。因此你会得到:exception ExceptionA is never thrown in body of corresponding try statement
。
回答by Duncan Jones
As pointed out in the comments, you cannot catch an exception that's not thrown by the code within your try
block. Try changing your code to:
正如评论中指出的那样,您无法捕获try
块内代码未引发的异常。尝试将您的代码更改为:
try{
Integer.parseInt(args[i-1]); // this only throws a NumberFormatException
}
catch(NumberFormatException e){
throw new MojException("Bledne dane");
}
Always check the documentationto see what exceptions are thrown by each method. You may also wish to read up on the subject of checked vs unchecked exceptionsbefore that causes you any confusion in the future.
回答by Mohit gupta
Always remember that in case of checked exception you can catch only after throwing the exception(either you throw or any inbuilt method used in your code can throw) ,but in case of unchecked exception You an catch even when you have not thrown that exception.
永远记住,在检查异常的情况下,您只能在抛出异常后才能捕获(抛出异常或代码中使用的任何内置方法都可以抛出),但是在未检查异常的情况下,即使您没有抛出该异常,您也会捕获到该异常。
回答by Astha
Any class which extends Exception
class will be a user defined Checked exception classwhere as any class which extends RuntimeException
will be Unchecked exception class.
as mentioned in User defined exception are checked or unchecked exceptionsSo, not throwing the checked exception(be it user-defined or built-in exception) gives compile time error.
任何扩展Exception
类的类都将是用户定义的 Checked 异常类,而任何扩展的类都RuntimeException
将是Unchecked 异常类。如用户定义的异常是已检查或未检查的异常中所述,因此,不抛出已检查的异常(无论是用户定义的还是内置的异常)会导致编译时错误。
Checked exceptionare the exceptions that are checked at compile time.
已检查异常是在编译时检查的异常。
Unchecked exceptionare the exceptions that are not checked at compiled time
未检查异常是在编译时未检查的异常