简体   繁体   English

在catch块中引发异常是否没有用?

[英]Is it useless to throw an exception in a catch block?

I am trying to figure out what is the best way to handle exceptions in java. 我试图找出什么是在Java中处理异常的最佳方法。 Is it a good idea to throw RunTimeException inside a catch block or it is useless? 将RunTimeException扔到catch块中是个好主意还是没有用?

public final void clickOnElement(MobileElement mobileElement, int secondsToWait) {
    try {
        abstractPlatform.clickOnElement(mobileElement,secondsToWait);
    } catch (Exception e) {
        throw new NoSuchElementException("Wasn't able to click on element " + mobileElement);
    }
}

Sometimes one indeed wants a RuntimeException instead of a checked exception. 有时确实需要RuntimeException而不是检查的异常。

} catch (Exception e) {
    throw new NoSuchElementException("Wasn't able to click on element " + mobileElement, e);
}

I would specify the exact exceptions, like IOException|SQLException , and add it as cause to the rethrow. 我将指定确切的异常,例如IOException|SQLException ,并将其添加为重新抛出的原因。

A recent use-case are lambdas that are in a context (streams) where checked exceptions are not allowed. 最近的用例是在不允许检查异常的上下文(流)中的lambda

Or simply to add additional info that might help pinpoint the error, like file name or SQL statement & parameters. 或者只是添加其他信息来帮助查明错误,例如文件名或SQL语句和参数。

Especially IllegalArgumentException and IllegalStateException can be more informative than other exceptions. 特别是IllegalArgumentException和IllegalStateException可以比其他异常提供更多信息。

It may or may not be useless, totally depends on how we use it. 它可能没有用也可能没有用,完全取决于我们如何使用它。 I have used it in a way so that I can maintain logs for exceptions. 我以某种方式使用它,以便可以维护异常日志。

public final void clickOnElement(MobileElement mobileElement, int secondsToWait) 
{
  try 
  {
    abstractPlatform.clickOnElement(mobileElement,secondsToWait);
  } 
  catch (Exception e) 
  {
    logError(this, "clickOnElement()", "Wasn't able to click on element"+mobileElement);
    throw new NoSuchElementException("Wasn't able to click on element " + mobileElement);
  }
}

Now in this case I have created a method that logged exception for that particular method and by looking at the logs I might be able to figure out the cause of exception. 现在,在这种情况下,我创建了一个记录该特定方法异常的方法,通过查看日志,我可能能够找出异常原因。 But lets assume that this piece of code is being used in an API. 但是让我们假设这段代码正在API中使用。

And as a API designer I want to return some specific status code instead of showing exception message send by the tomcat/framework. 作为一名API设计人员,我想返回一些特定的状态代码,而不是显示Tomcat /框架发送的异常消息。 In that case we can throw the exception in catch and that exception can be held in some parent method or by the framework itself if there is some mechanism in place for that. 在那种情况下,我们可以将异常扔到catch中,并且可以使用某种父方法或框架本身(如果有适当的机制)来保存异常。

So its all upto how we use it. 因此,这取决于我们如何使用它。

No its not useless, I do this a lot in code I write I would rather see my own custom exception with a message about the error, since you are handling where it is caught in most of those cases you will know why it has thrown, so I much prefer to see an exception with a detailed message about the exact issue instead of a generic exception been thrown and just seeing an error message in the console, an example of something useless would be 没错,它不是没有用的,我会在编写的代码中做很多事情,我宁愿看到我自己的自定义异常以及有关该错误的消息,因为在大多数情况下,您正在处理它的捕获位置,因此您会知道它为什么被抛出,因此,我更希望看到带有有关确切问题的详细消息的异常,而不是引发一般异常,而只是在控制台中看到错误消息,这样的事例是无用的

try {
    abstractPlatform.clickOnElement(mobileElement,secondsToWait);
} catch (Exception e) {
    throw e;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM