The Java language requires that all thrown (throw) exceptions from the Java program (regardless of who wrote the code) must be derived from Throwable. Of course, in actual Java programming, because the JDK platform has designed a very rich and complete anomaly object classification model for us. As a result, Java programmers generally do not need to redefine their own exception objects. And even if you need to extend your custom exception objects, you tend to derive from exception. So, for a Java programmer, it generally only needs to catch (Exception ex) in its top-level function to catch all of the exception objects. The base class for all the exception objects is Throwable, Throwable is inherited directly from object (which is mandatory by the Java system), and it implements the Serializable interface (This provides the most adequate material preparation for all exception objects that can easily cross the Java component System). The exception classes derived directly from the Throwable are exception and error. Exception is the most familiar of Java programmers, and it generally represents the basic class of objects in the real sense of the exception. That is to say, Exception and all exceptions derived from it are the types of exceptions that an application can catch, and that can perform exception recovery processing. The error indicates that a very serious exception occurred in the Java system, and that it may not be recoverable by the application, such as Linkageerror, or Threaddeath.
error and RuntimeException subclasses are unchecked , that is, there is no need to explicitly throws Error can also be compiled through. The
runtimeexception indicates that the occurrence of such an exception is often unpredictable and, if it occurs, aborts processing.
error generally indicates an unrecoverable failure, that is, you will still have to try again, such as outofmemoryerror, how many times a large probability of error. The
exception represents a generic exception, and this exception is a common error that has long been considered in the design and must be described in the design if the exception is handled. The
runtimexception is closer to "surprises" in our daily lives.
exception This is closer to what we call "common mistakes" in our daily lives.
error is more like "hell, it seems that the weekend does not work overtime is uncertain."
Technically,error/runtimeexception and Exception only unchecked and checked This compilation process is different, the main difference between them is in the design of its important program and processing routines.
It is as if sometimes, we are saying that the A->B and B<-A effects are different, indicating that they are the same, but sufficient or necessary or sufficient We can not because a requirement implementation is complex and choose a workaround to the requirements, this confusion I found in other people's code quite a lot of such errors, so that the program much more bug.
Throwable is a class in the Java.lang package that is designed to handle exceptions. It has two subclasses, namely, error and exception, which are used to handle two sets of exceptions, respectively.
The error is used to handle exceptions to the environment in which the program is running, such as virtual machine errors, mount errors, and connection errors, which are primarily hardware-related and not thrown by the program itself.
exception is a primary subclass of Throwable. Exception is also below the subclass, where some of the subclasses correspond to the handling of various exceptions that are frequently encountered by Java programs, including implicit exceptions. For example, the program in the divisor of 0 error, array subscript error, etc., such as the exception is also known as run-time exceptions, because they are caused by the program itself, but not the program unsolicited, but in the operation of the program generated.
The other subset of subclasses below the Exception subclass corresponds to the handling of non run-time exceptions in Java programs (which are directly in the Exception in the following illustration), which are also called explicit exceptions. They are all in the program with the statement thrown, and is also used to capture the statement, for example, the file did not find the cause of the exception, the class did not find the exception caused by, and so on.
Some of the main subclasses correspond to the exception handling function briefly described below:
arithmeticexception--due to the exception caused by the divisor of 0;
arraystoreexception--because the array storage space is not enough to cause the anomaly;
classcastexception-when you classify an object as a class, but the object is not created by this class, and it is not created by its subclasses, it can cause an exception;
illegalmonitorstateexception--the error caused by the state of the monitor;
negativearraysizeexception-the length of an array is negative, an exception is generated;
nullpointerexception-An exception occurs when a program attempts to access an element in an empty array or accesses a method or variable in an empty object;
outofmemoryexception--when an object is created with the new statement, an exception is generated if the system cannot allocate memory space for it;
securityexception--due to access to the pointer should not be accessed, so that security problems caused by the exception;
indexoutofboundsexcention--an exception is caused by an array subscript out of bounds or a string access bounds;
ioexception--caused an exception because the file was not found, was not opened, or I/O operation could not be performed;
classnotfoundexception--a class or interface that does not find the specified name causes an exception;
clonenotsupportedexception--an object in a program references the Clone method of the object class, but this object does not connect the Cloneable interface, causing an exception;
interruptedexception-when one thread is in a waiting state, another thread interrupts the thread, causing an exception, and the contents of the thread are described in the next chapter;
Nosuchmethodexception a method called is not found, causing an exception;
illega1accessexception-an attempt to access a Non-public method;
stringindexoutofboundsexception--Access string serial number out of bounds, causing an exception;
arrayidexoutofboundsexception-an array element to access the subscript bounds, causing anomalies;
numberformatexception--character of the UTF code data format error caused by the exception;
illegalthreadexception-a thread to call a method and the state is not appropriate, causing an exception;
filenotfoundexception--the specified file was not found to cause an exception;
eofexception--does not complete the input operation is the end of the file caused an exception.
The error class and the parent class of the exception class are all Throwable classes, and their differences are:
The error class generally refers to problems related to virtual machines, such as system crashes, virtual machine errors, insufficient memory, method call stack overflow, and so on. For this type of error caused by the application interruption, only the program itself can not recover and and prevention, encountered such an error, it is recommended to terminate the program.
The exception class represents an exception that a program can handle, and can be caught and possibly recovered. When you encounter such an exception, you should handle the exception as much as possible so that the program resumes running, rather than terminating the exception at will.
The Exception class is divided into Run-time exceptions (Runtime Exception) and checked exceptions (Checked Exception), run-time exceptions; Arithmaticexception,illegalargumentexception, the compiler can pass, but a run is terminated, the program does not handle run-time exceptions, this type of exception, the program will terminate. The exception that is checked is either a try ... Catch capture, either thrown with the throws clause declaration and handed over to its parent class, otherwise the compilation will not pass.
Difference between error and exception (Error vs Exception) Today, the interview asked such a question, "error" and "Exception" What is the difference between. I think it's interesting, but I can't seem to blurt it out. After looking up some information, I summed it up a little. 1 Error is inherited from the parent class Java.lang.Error, and Exception are inherited from Java.lang.Exception.
2) and then look at the JDK's explanations for java.lang.Error and java.lang.Exception.Java.lang.Error: An Error are a subclass of throwable that indicates serious problems this a reasonable application should not try to CATC H. Most such errors are abnormal conditions. That is, error is a subclass of Throwable, used to mark critical errors.a reasonable application should not try/catch this error. Most of the errors are abnormal and should not be present at all. Java.lang.Exception: The class Exception and its subclasses are a form of throwable this indicates conditions that a reasonable application m Ight want to catch. That is, exception is a form of subclass of Throwable, used to indicate a reasonable program to catch the conditions.that is, it is only a program operating condition, not a serious error, and encourages the user program to catch it. 3 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 is usually thrown out of a recoverable program, and it is best to use the program recovery from this exception. such as FileNotFoundException, ParseException and so on.Unchecked Exceptions:It's usually the case that if all the normal scripts don't happen, it does. 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.