Java Anomaly Mechanism Analysis _java

Source: Internet
Author: User

This article analyses the exception mechanism of Java and share it for everyone's reference. Believe that help everyone to improve the Java program exception handling ability. The specific analysis is as follows:

As we all know, the Java exception (Exception) mechanism is very important, the program will inevitably error, the exception mechanism can catch errors in the program, to improve the stability and robustness of the program.

The exceptions in Java are divided into checked Exception (non-Runtime Exception) and unchecked Exception (Runtime Exception), all of which inherit Exception directly or indirectly. Exception and directly inherit from Throwable class, Throwable class and the direct subclass of the error class, but when the error, the program will be directly out of execution, the program can not handle error, so here is the focus of the discussion exception class. The RuntimeException class inherits from the exception, and the Run-time exception inherits from the RuntimeException class directly or indirectly. Other runtime exceptions that inherit from the exception class are discovered during compilation. Runtime exceptions occur at run time, such as arithmetic except 0, which can be passed during compilation, but at run time throws a Run-time exception, and the divisor cannot be zero.

There are two kinds of Java exception handling

1, catch exception

structure is as follows

Try
{
//execute code that may produce an exception-
catch (runtimeexception e)
{
//deal with caught exceptions
}
catch ( Exception e)/can define capturing multiple exceptions
{
//processing of caught exceptions
}
finally
{
//The code to be executed after the capture is done, dispensable, if any will be executed, With or without exception
}
try-catch-finally

If the exception is caught by catch, then a catch statement is not executed, but if there is finally, the statement inside the finally block is executed.

2, the declaration throws an exception

Throws Exception is declared at the method declaration where the method that produces the exception is invoked, and the method does not process the class exception, but is handled by the caller of the method.

3, artificial throw out the exception

Throw the exception directly in the method, throw exception;

The runtime exception recommendation is not processed.

A custom exception defines a subclass that inherits from the exception class, and generally does not define an inherited from RuntimeException class, and when a return statement is present in a try block, if there is a finally block statement, You also have to execute the statement of the finally block before you return, but if there is a system.exit (0) statement in the TRY block, the statement of the finally block is not executed because System.exit (0) terminates the currently running Java Virtual machine. The program finishes execution before the virtual machine terminates.

Class MyException extends Exception
{public
  myexception (String str)
  {
    super (str);
  }
}
MyException

Exceptions can be used in file processing, input and output stream applications, and so on.

Common types of exceptions are:

1, java.lang.NullPointerException: null pointer exception, the reason for this exception is a reference is NULL, but the program also called a method of that reference.

2, Java.lang.ClassNotFoundException: The specified class could not be found, possibly the class is undefined.

3, Java.lang.ArithmeticException: arithmetic anomalies, such as zero divisor.

4, FileNotFoundException: In the file processing, the specified file could not be found.

5, IOException: input and output flow anomaly

6, Sqlexception:sql exception, execute SQL statement error.

There are also many types of exceptions that will be encountered in future programming. It is believed that this article has certain reference value to everybody's Java program design.

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.