Common exceptions in java

Source: Internet
Author: User


What are the differences between runtime exceptions and general exceptions:

An exception indicates an abnormal state that may occur during the running of the program,Running exceptions indicate exceptions that may be encountered during common operations on virtual machines, which is a common running error.. The java compiler requires that methods must declare and throw possible non-runtime exceptions, but do not require that they throw uncaptured runtime exceptions.

Runtime exception:
Except, ArrayStoreException, except, failed, CannotRedoException, CannotUndoException, ClassCastException, cmcmexception, except, DOMException, EmptyStackException, except, failed, ImagingOpException, failed, MissingResourceException, failed, failed, nullPointerException, ProfileDataException, ProviderException, RasterFormatException, SecurityException, SystemException, UndeclaredThrowableException, UnmodifiableSetException, UnsupportedOperationException

General Exception: IOException FileNotFoundException SqlException


What is the difference between error and exception:

Error indicates that recovery is not a serious problem that is impossible but difficult. For example, memory overflow. It is impossible to expect the program to handle such a situation. Exception indicates a design or implementation problem. That is to say, it indicates that if the program runs normally, it will never happen.

The simple principle and application of the exception handling mechanism in Java:

Exceptions are exceptions or errors that occur when java programs are running (not compiled). They are similar to events in real life, events in real life can contain information such as the time, location, character, and plot of an event, which can be expressed as an object, Java uses an object-oriented method to handle exceptions. It encapsulates every exception in the program into an object for representation. This object contains the exception information..
Java classifies exceptions. Different types of exceptions are represented by different Java classes, The root class of all exceptions is java. lang. Throwable., Throwable, two subclasses are derived: Error and Exception, Error indicates that the application itself cannot overcome and recover a serious problem. The program only has dead parts, for example, memory overflow, thread deadlock, and other system problems. Exception indicates that the program can also overcome and recover the problems, including system exceptions and Common exceptions. system exceptions are caused by Software defects, that is to say, software developers cannot overcome and recover the problems caused by weeks of consideration, but in this case, the software system can continue to run or let the software die.For example, the array script is out of bounds (ArrayIndexOutOfBoundsException), the NULL pointer is abnormal (NullPointerException), and the class conversion is abnormal (ClassCastException ); Common exceptions are caused by changes in the running environment or exceptions, and they can be overcome by users.For example, if the network is disconnected and the hard disk space is insufficient, the program should not die after such an exception occurs.
Java provides different solutions for system exceptions and Common exceptions, The compiler forces normal exceptions to be handled by try... catch or throws declaration to continue to be thrown to the upper-layer call method for handling., So A common exception is also called a checked exception., And System exceptions can be handled or not handled. Therefore, the compiler does not force try .. catch to be processed or throws to declare them. Therefore, system exceptions are also called unchecked exceptions..

Common exception handling:


Try: execution part, causing exceptions
Catch: Catch exceptions
Finally: run the command no matter whether there are any exceptions.
Throws: Declares the exception to be thrown in the method declaration. The caller must handle the exception.
Throw: throws an exception.
An exception can be thrown in try, which is generally the same as the declared exception.

Try {
// Exceptions may occur
} Catch (Type1 id1 ){
// Handle exception 1
}
Catch (Type2 id2 ){
// Handle exception 2
}
...
Finally
{
// The part that is always executed regardless of whether an exception occurs
}


Custom exceptions must inherit the subclass of Exception or Exception.

Package java. lang;

// Referenced classes of package java. lang:
// Throwable, String

Public class Exception extends Throwable
{

Public Exception ()
{
}

Public Exception (String s)
{
Super (s );
}

Public Exception (String s, Throwable throwable)
{
Super (s, throwable );
}

Public Exception (Throwable throwable)
{
Super (throwable );
}

Static final long serialVersionUID =-3387516993124229948L;
}

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.