Java 可以抛出运行时异常吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19475584/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 17:35:38  来源:igfitidea点击:

Runtime Exception can be thrown?

javaexceptionruntimeexception

提问by Bhargav

I have written a class in which i am trying to throw a runtime exception. The code is as follows

我写了一个类,我试图在其中抛出运行时异常。代码如下

public class ExceptionTest 

{

    public static void ReadFile() throws RuntimeException, FileNotFoundException{
    try{

    BufferedReader b =new BufferedReader(new FileReader("I:\Workspace\Basic Java\bin\Exceptions\List To Read.txt"));
    String s = b.readLine();
    while(s!=null){
        System.out.println(s);
        s=b.readLine();
    }
    }
    catch(RuntimeException e){
        throw e;
    }
    catch(FileNotFoundException e){
        throw e;
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    }

And my main class looks like

我的主班看起来像

public class TheMain {
public static void main(String args[]){
    ExceptionTest.ReadFile();
}
}

my doubt is can we throw a run time exception? Please help

我的疑问是我们可以抛出运行时异常吗?请帮忙

采纳答案by Srikanth Reddy Lingala

RunTimeExceptionis an unchecked exception. You can throw it, but you don't necessarily have to, unless you want to explicitly specify to the user of your API that this method can throw an unchecked exception. But even this doesn't seem to be necessary if you mention this fact (that the method can throw a RunTimeException) in the javadoc for this method.

RunTimeException是一个未经检查的异常。您可以抛出它,但不一定非得如此,除非您想向 API 的用户明确指定此方法可以抛出未经检查的异常。但是,如果您在此方法的 javadoc 中提到这一事实(该方法可以抛出 RunTimeException),即使这似乎也没有必要。

So, in short, yes, you can throw it, but you don't have to, as it does not provide you any given advantage and costs you a few extra lines of code

因此,简而言之,是的,您可以抛出它,但您不必这样做,因为它不会为您提供任何给定的优势,并且会花费您一些额外的代码行

回答by Abrixas2

You can throw a java.lang.RuntimeException(or any derived exception), as it is a normal exception. The difference to other exceptions is that you do not need to mention it in the list of thrown but uncaught exceptions a method can throw, ie.

您可以抛出一个java.lang.RuntimeException(或任何派生的异常),因为它是一个正常的异常。与其他异常的不同之处在于,您不需要在方法可以抛出的抛出但未捕获的异常列表中提及它,即。

public void foo(int bar) throws RuntimeException {
    throw new RuntimeException("foo(bar)");
}

has the same effect as

具有相同的效果

public void foo(int bar) {
    throw new RuntimeException("foo(bar)");
}

Despite that, a java.lang.RuntimeException(or anything derived) behaves like a normal exception, ie. it terminates your program, if not caught.

尽管如此,a java.lang.RuntimeException(或任何派生的)表现得像一个正常的异常,即。如果没有被捕获,它会终止您的程序。

回答by mo sean

throw new RuntimeException('someMessagehere')

回答by Rakesh

Ideally Runtime exception should never be thrown deliberately. Java has categorised exception in Checked Exception and Unchecked i.e Runtime exception. We should always throw checked exception. The unchecked exceptions should never occur in the program as they are supposed to be handled programmatically and not by throwing them.

理想情况下,不应故意抛出运行时异常。Java 已将异常分类为已检查异常和未检查即运行时异常。我们应该总是抛出检查异常。未经检查的异常不应该出现在程序中,因为它们应该以编程方式处理,而不是通过抛出它们。