Java----Exception Class (Error and exception, difference) __java

Source: Internet
Author: User
Tags exception handling throwable

In Java, all exceptions have a common ancestor throwable (can be thrown). Throwable specifies the commonality of any problems that can be transmitted through a Java application by using the exception propagation mechanism available in the code.
Throwable has two important subclasses: Exception (Exception) and error (Errors), both of which are important subclasses of Java exception handling, each containing a large number of subclasses.
①.exception (exceptions) are possible predictable, recoverable issues in your application. In general, most anomalies represent moderate to mild problems. Exceptions are typically generated in a particular environment, usually in specific methods and operations of the code. In the Echoinput class, a IOException exception can occur when an attempt is made to invoke the ReadLine method.
The Exception class has an important subclass of RuntimeException. The RuntimeException class and its subclasses represent errors raised by the JVM common operations. For example, if an attempt is made to use a null-valued object reference, a divisor of zero, or an array out of bounds, a Run-time exception (NullPointerException, ArithmeticException) and Arrayindexoutofboundexception are raised.
②.error (Error) indicates a more serious problem in running the application. Most errors have nothing to do with what the code writer does, but represent a problem with the JVM (the Java virtual machine) when the code is running. For example, outofmemoryerror occurs when the JVM no longer has the memory resources it needs to continue to perform operations.

Check for exceptions and the partition of unchecked exceptions

Exceptions in Java are grouped into two broad categories:
1.Checked Exception (non-runtime Exception)
2.Unchecked Exception (Runtime Exception)
Run-time exceptions
The RuntimeException class is a subclass of the exception class, which is called a Run-time exception, and all Run-time exceptions in Java are inherited directly or indirectly from the RuntimeException class.
Exceptions in Java that inherit from exception and do not inherit from RuntimeException classes are run-time exceptions.
A try can be followed by more than one catch, but no matter how many, a maximum of one catch block is executed.

For non-run-time exceptions (checked exception), it must be processed, otherwise it cannot be compiled.
There are two ways of handling:
1. Use Try ... Catch.. finally to capture;
2. After the method declaration that produces the exception, write throws a Exception type, such as throws Exception, throws the exception to the outer layer.
For Run-time exceptions (runtime exception), they can be processed or not handled. It is recommended that no run-time exceptions be processed.

extensions: Differences between errors and exceptions (Error vs Exception)
1. A subclass of java.lang.Error:Throwable that marks a fatal error. A reasonable application should not try/catch this error. Most of the errors are abnormal and should not be present at all.
A subclass of java.lang.Exception:Throwable, used to indicate a condition where a reasonable program wants to catch. That is, it is only a program operating condition, not a serious error, and encourages the user program to catch it.
2). Both the error and runtimeexception and their subclasses are unchecked exceptions (unchecked exceptions), and all other exception classes are checked exceptions (checked exceptions).

Checked exceptions: It's usually thrown out of a recoverable program, and it's best to use program recovery from this exception. such as FileNotFoundException, ParseException and so on. The exception that is checked occurs at compile time and must be used Try...catch (or throws) otherwise the compilation does not pass.
Unchecked exceptions: usually if all the normal scripts don't happen, it does happen. Occurs in the operational period, has the uncertainty, mainly is because of the procedure logic question causes. such as Arrayindexoutofboundexception, classcastexception and so on. From the language itself, the program should not catch such exceptions, although it can catch and recover from exceptions such as runtimeexception, but it does not encourage end programmers to do so, because there is absolutely no need. Because such errors are bugs in themselves, they should be fixed and the program should stop executing immediately if such errors occur. Therefore, in the face of errors and unchecked exceptions should let the program automatically terminate execution, the programmer should not do things such as try/catch, but should find out why, modify the code logic.
The runtimeexception:runtimeexception system includes incorrect type conversions, array access across arrays, and attempts to access null pointers, and so on.
The principle of dealing with runtimeexception is that if there is a runtimeexception, it must be the programmer's fault. For example, you can avoid array-crossing access exceptions by examining the array subscripts and arrays of boundaries. Other (IOException et cetera) checked exceptions are generally external errors, such as trying to read data from the end of a file, and so on, which is not an error in the program itself, but an external error that occurs in the application environment.














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.