In-depth error capture in Java

Source: Internet
Author: User

Java error can capture this problem. I have seen it from other blogs and tried it myself. In fact, error and exception can be captured. But some time ago, I started to discuss it with my colleagues. Compared with other people's articles, I only wanted to validate and capture it. I wanted to add something different. So I wrote this article.
The following code is the code caught when an exception occurs.

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");}

Run this method and the log appears.

java.lang.Error: An error occurs hereat ps.androidyue.catcherror.MainRoot.testCatchError(MainRoot.java:10)at ps.androidyue.catcherror.MainRoot.main(MainRoot.java:5)catched as Errorreach the end of the method

From the above log, we can clearly analyze the error object we throw without exception catch. Instead, it enters the catch of error and is captured successfully, in this way, the output at the end can be executed downward.
So the error level is higher than the exception level or is the two in parallel? What is throwable next?
First, let's take a look at throwable. From javadoc, we can see that it is the super class of the object instance that the VM can throw. The relationship between error and exception is parallel. For example, the exception. printstacktrace () that we often call is in throwable.

/** * 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}

Part of the Exception Code

/** * {@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}

Error Code

/** * {@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}

We can use try-catch to capture exceptions and errors, or capture throwable at a larger level, which is not desirable. We can use try-catch to catch exceptions, because these exceptions are generally generated by application code. However, errors cannot be captured by program code. Except for a few exceptions, such as outofmemoryerror.

So why do we need to use try-catch to avoid exceptions?
Java has two types of exceptions: runtimeexception and non-runtimeexception.

Runtimeexception is an unchecked exception and does not require try-catch.

Common runtimeexception include nullpointerexception.
Instead of runtimeexception, It is a checked exception and you need to use try-catch to check

Another problem is that exception is recoverable, and error is unrecoverable, which is not very clear at present. Next, I will explain it in another article.

Recommended articles read: http://learnwithharsha.com/exceptions/

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.