赞
踩
- private static void testCatchError() {
- try {
- throw new Error("An error occurs here");
- } catch (Exception e) {
- System.out.println("catched as Exception");
- e.printStackTrace();
- } catch (Error e) {
- System.out.println("catched as Error");
- e.printStackTrace();
- } catch (Throwable t) {
- System.out.println("catched as Throwable");
- t.printStackTrace();
- }
-
- System.out.println("reach the end of the method");
- }

- java.lang.Error: An error occurs here
- at ps.androidyue.catcherror.MainRoot.testCatchError(MainRoot.java:10)
- at ps.androidyue.catcherror.MainRoot.main(MainRoot.java:5)
- catched as Error
- reach the end of the method
- /**
- * The superclass of all classes which can be thrown by the VM. The
- * two direct subclasses are recoverable exceptions ({@code Exception}) and
- * unrecoverable errors ({@code Error}). This class provides common methods for
- * accessing a string message which provides extra information about the
- * circumstances in which the {@code Throwable} was created (basically an error
- * message in most cases), and for saving a stack trace (that is, a record of
- * the call stack at a particular point in time) which can be printed later.
- * <p>
- * A {@code Throwable} can also include a cause, which is a nested {@code
- * Throwable} that represents the original problem that led to this {@code
- * Throwable}. It is often used for wrapping various types of errors into a
- * common {@code Throwable} without losing the detailed original error
- * information. When printing the stack trace, the trace of the cause is
- * included.
- *
- * @see Error
- * @see Exception
- * @see RuntimeException
- */
- public class Throwable implements java.io.Serializable {
- //code goes here
- }

- /**
- * {@code Exception} is the superclass of all classes that represent recoverable
- * exceptions. When exceptions are thrown, they may be caught by application
- * code.
- *
- * @see Throwable
- * @see Error
- * @see RuntimeException
- */
- public class Exception extends Throwable {
- //code goes here
- }
- /**
- * {@code Error} is the superclass of all classes that represent unrecoverable
- * errors. When errors are thrown, they should not be caught by application
- * code.
- *
- * @see Throwable
- * @see Exception
- * @see RuntimeException
- */
- public class Error extends Throwable {
- //code goes here
- }
RuntimeException是unchecked的异常,不需要使用try-catch
常见的RuntimeException有NullPointerException等Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。