Java中的捕获与抛出异常 [英] Catching versus Throwing Exceptions in Java

查看:621
本文介绍了Java中的捕获与抛出异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我总体上有两个关于Java的一般性问题。第一个是何时会在方法主体中使用try / catch而不是在声明方法时使用throws异常?这是我的意思的一些证明。这:

 公共无效,而Chatting()抛出IOException {} 

相对

  public void closeConnection(){
try {
} catch(IOException ioException){
ioException.printStackTrace();
}
}

然后我的第二个问题是何时知道捕获或抛出的异常类型?我的意思是说,例如IOException或EOFException之类的异常...



如果有一个很好的链接,有人可以让我教所有这一切(因为这可能更复杂)比我想的要多。我会很感激,就像您回答了一样。
谢谢。

解决方案

抛出异常



在您的第一个示例,

 公共无效,而Chatting()抛出IOException {} 

表示它只会向调用函数的对象抛出异常。然后可以在使用try-catch块调用该方法时将其捕获。例如

  try {
whileChatting();
}
catch(IOException e){
e.printStackTrace();
}

抛出一个方法基本上会在整个链中传播,因此任何调用方法此方法还需要包含 throws IOException ,或者需要在更高级别的方法中处理该异常(通常通过try-catch块进行处理)。 / p>

捕获异常



捕获异常是一种很好地处理异常的方法。常见的操作是 e.printStackTrace(); ,该错误将错误的详细信息打印到控制台,但是并非总是必要的。有时您可能想执行 System.exit(0)或什至 System.out.println(方法whileCalling()中的错误)



使用catch块,您可以捕获任何类型的异常!您还可以执行try-catch-finally块,该块将运行try块中的代码,捕获任何异常,以及是否捕获到任何异常,它将进入finally块并运行该代码。



要了解可能需要捕获的异常,可以查看可能引发异常的类的Javadocs。在这里,您会找到该类可能抛出的所有可能事件的列表。您还可以捕获 Exception ,它将捕获任何可以想象的异常! (好吧,因为它扩展了异常)



最后,您可以像这样将catch块链接在一起。

  try {
whileCalling();
}
catch(IOException e){
//处理这种情况
}
catch(FileNotFoundException e){
//处理这种情况
}
catch(Exception e){
//处理这种情况
}

这将与和else-if一样工作,并且不会捕获重复项。



因此,基本上可以回答您的问题:



1::要抛出 ,异常意味着要让其他人来处理它,无论这是另一个类还是另一个方法,或者只是希望它不会发生,否则您的程序会崩溃(非常糟糕的做法)。



2: catch块实际上是处理异常,但是您认为合适!打印出堆栈跟踪以进行调试,或者给用户一个新的提示以重新输入某些内容。 (例如,提示用户需要输入文件位置,然后引发 FileNotFoundException



希望有帮助!


So I have two general questions about java in general. The first is when would one use a try/catch in the body of the method versus using throws exception in declaring the method? Here is a little demonstration of what I mean. This:

public void whileChatting() throws IOException{}

versus

public void closeConnection() {
    try {
    } catch (IOException ioException) {
        ioException.printStackTrace();
    }
}

And then my second question is when does one know what type of exception to either catch or throw? By that I mean exceptions such as IOException or EOFException and so on...

If there's a good link someone could send me teaching all this (being that it's probably more complicated than I think) I would be just as grateful as if you answered it. Thanks.

解决方案

Throwing Exceptions

In your first example,

public void whileChatting() throws IOException{}

means that it will just throw the exception to whatever is calling the function. It can then be caught while calling that method with a try-catch block. such as

try{
    whileChatting();
}
catch(IOException e){
    e.printStackTrace();
}

Throwing a method basically propagates it up the chain, and so any method that calls this method will need to also include throws IOException, or the exception will need to be dealt with in the higher level method (by means of a try-catch block usually).

Catching Exceptions

Catching an exception is a way to gracefully deal with exceptions. The common thing to do is e.printStackTrace(); which prints details of the error to the console, however it's not always necessary. Sometimes you may want to do a System.exit(0) or even a System.out.println("Error in method whileCalling()")

With a catch block you can catch any type of exception! you can also do a try-catch-finally block which will run the code in the try block, catch any exceptions, and whether or not any exceptions are caught it will enter the finally block and run that code.

To know what Exception you might need to catch, you can look at the Javadocs for the class that may throw the exception. There you will find a list of every possible thing that the class can throw. You can also just catch Exception which will catch any Exception imaginable! (Well, granted it extends Exception)

And finally you can link catch blocks together like so.

try{
    whileCalling();
}
catch(IOException e){
    //handle this situation
}
catch(FileNotFoundException e){
    //handle this situation
}
catch(Exception e){
    //handle this situation
}

This will work like and else-if block and not catch duplicates.

So to answer your questions basically:

1: To throw an Exception means to have somebody else deal with it, whether this be another class, another method, or just to hoping it doesn't happen or else your program will crash(pretty bad practice).

2: To use a try catch block is to actually deal with the Exception however you see fit! printing out the stacktrace for debugging or giving the user a new prompt to re-input something maybe. (For instance a prompt that the user needs to enter a file location and then it throws a FileNotFoundException)

Hope that helps!

这篇关于Java中的捕获与抛出异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆