Java exception mechanism analysis

  • 2020-04-01 03:29:10
  • OfStack

This article analyzes the Java example of the exception mechanism, to share with you for your reference. I believe it will help you to improve your Java program exception handling ability. Specific analysis is as follows:

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

Exceptions in Java are divided into Checked Exception(non-runtime Exception) and UnChecked Exception(Runtime Exception). All Exception classes directly or indirectly inherit exceptions. Exception is directly inherited from the Throwable class, and there is an error class in the direct subclass of the Throwable class. However, when an error occurs, the program will exit the execution directly, and the program cannot handle the error. Therefore, the focus is on the Exception class. The RuntimeException class inherits from Exception, and runtime exceptions inherit directly or indirectly from the RuntimeException class. Other non-runtime exceptions that inherit from the Exception class are found at compile time. Run-time exceptions occur only at run time, such as arithmetic division by zero, which is passable at compile time, but at run time a run-time exception is thrown and the divisor cannot be zero.

There are two types of exception handling in Java

1. Catch exceptions

Structure is as follows


try
{
//Execute code that might produce an exception

}
catch(RuntimeException e)
{
//Handle the caught exception
}
catch(Exception e)//Multiple exceptions can be defined to catch
{
//Handle the caught exception
}
finally
{
//After capturing the code to be executed, optional, if any, it will be executed, with or without exceptions
}
try-catch-finally

If the exception is caught, the catch statement behind it will not be executed, but if there is a finally, the statement in the finally block must be executed.

2. Declare an exception

Declaring a throws Exception at the method declaration where the method that will generate the Exception is called does not handle the Exception, but is handled by the caller of the method.

3. Throw an exception artificially

Throw an exception directly in the method, throw exception;

It is recommended that runtime exceptions not be handled.

Custom Exception is defined a subclass of inherited from the Exception class, generally do not define inherited from RuntimeException class, when there is a return statement in the try block, if there is a finally block statements, so also need to perform after finally block statements and back, but if you try block exists in the System. The exit (0) statement, will not perform the finally block statements, because of the System. The exit (0) will terminate the currently running the Java virtual machine, The program will terminate execution before the virtual machine terminates.


class myException extends Exception
{
  public myException(String str)
  {
    super(str);
  }
}
myException

Exceptions are used in file processing, input/output stream applications, etc.

Common types of exceptions are:

1, Java. Lang. NullPointerException: null pointer exception, the reasons for the exception is a reference to null, but in the program to invoke a method of the reference.

2, Java. Lang. ClassNotFoundException: can't find the specified class, may be the class is undefined.

3, Java. Lang. ArithmeticException: arithmetic exception, such as dividing by zero.

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

5. IOException: input/output stream exception

6. SQLException: SQL exception, error occurred in executing the SQL statement.

There are also many types of exceptions that will be encountered in future programming. I believe that this article has a certain reference value for everyone's Java programming.


Related articles: