当前位置:   article > 正文

Java中Error捕获深入_try catch 能抓error吗

try catch 能抓error吗
Java Error能不能捕获这个问题,之前从别人的博客看到过,然后自己也实验过,其实Error和Exception一样是可以捕获的。但是前些时候,又开始和同事讨论了起来,相比之前别人的文章,只是验证可以捕获,想加入点不同的东西。所以写下这篇文章。
首先下面的代码就是出现异常并捕获的代码。

  1. private static void testCatchError() {
  2. try {
  3. throw new Error("An error occurs here");
  4. } catch (Exception e) {
  5. System.out.println("catched as Exception");
  6. e.printStackTrace();
  7. } catch (Error e) {
  8. System.out.println("catched as Error");
  9. e.printStackTrace();
  10. } catch (Throwable t) {
  11. System.out.println("catched as Throwable");
  12. t.printStackTrace();
  13. }
  14. System.out.println("reach the end of the method");
  15. }


运行这个方法,日志出现了。

  1. java.lang.Error: An error occurs here
  2. at ps.androidyue.catcherror.MainRoot.testCatchError(MainRoot.java:10)
  3. at ps.androidyue.catcherror.MainRoot.main(MainRoot.java:5)
  4. catched as Error
  5. reach the end of the method


从上面的日志我们可以很明确的分析出来,我们throw出的Error对象,没有经过Exception的catch,而是进入了Error的catch并成功捕获,进而可以向下执行到末尾的输出。
那么Error比Exception的层级高还是两者并列?后面的Throwable又是什么呢
首先我们了解一下Throwable,从javadoc可以看出是VM可以抛出的对象实例的超类,Error和Exception是并列的关系。比如我们常常调用的exception.printStackTrace() 就是Throwable中的。


  1. /**
  2. * The superclass of all classes which can be thrown by the VM. The
  3. * two direct subclasses are recoverable exceptions ({@code Exception}) and
  4. * unrecoverable errors ({@code Error}). This class provides common methods for
  5. * accessing a string message which provides extra information about the
  6. * circumstances in which the {@code Throwable} was created (basically an error
  7. * message in most cases), and for saving a stack trace (that is, a record of
  8. * the call stack at a particular point in time) which can be printed later.
  9. * <p>
  10. * A {@code Throwable} can also include a cause, which is a nested {@code
  11. * Throwable} that represents the original problem that led to this {@code
  12. * Throwable}. It is often used for wrapping various types of errors into a
  13. * common {@code Throwable} without losing the detailed original error
  14. * information. When printing the stack trace, the trace of the cause is
  15. * included.
  16. *
  17. * @see Error
  18. * @see Exception
  19. * @see RuntimeException
  20. */
  21. public class Throwable implements java.io.Serializable {
  22. //code goes here
  23. }

Exception的部分代码
  1. /**
  2. * {@code Exception} is the superclass of all classes that represent recoverable
  3. * exceptions. When exceptions are thrown, they may be caught by application
  4. * code.
  5. *
  6. * @see Throwable
  7. * @see Error
  8. * @see RuntimeException
  9. */
  10. public class Exception extends Throwable {
  11. //code goes here
  12. }


Error的部分代码
  1. /**
  2. * {@code Error} is the superclass of all classes that represent unrecoverable
  3. * errors. When errors are thrown, they should not be caught by application
  4. * code.
  5. *
  6. * @see Throwable
  7. * @see Exception
  8. * @see RuntimeException
  9. */
  10. public class Error extends Throwable {
  11. //code goes here
  12. }



那么我们可以使用try-catch捕获异常,错误,亦或者使用从更大层面捕获Throwable,这一点是不可取的。我们使用try-catch捕获异常是可以,因为这些异常一般是Application code产生的。但是像错误不应该被程序代码捕获。极个别的除外,比如说OutOfMemoryError.

那么为什么有点异常需要强制使用try-catch,有的则不需要?
java中有两种大类型的异常,一种是RuntimeException,另一种是非RuntimeException。

RuntimeException是unchecked的异常,不需要使用try-catch

常见的RuntimeException有NullPointerException等
而非RuntimeException是checked异常,需要使用try-catch 来check


还有一个问题,就是关于Exception是recoverable,而Error是unrecoverable这个目前不是很清楚,下来研究另文说明。




推荐相关文章阅读:http://learnwithharsha.com/exceptions/
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/252895?site
推荐阅读
相关标签
  

闽ICP备14008679号