The fourth note Java exception

Java exception structure diagram

 

 

All from Java exceptions are Throwable inherited, Throwable has two subclasses, Error and Exception.

Error is a mistake, for all compile-time errors and system errors are thrown by Error. These errors indicate a failure in its own virtual machine, or VM occurs when trying to execute applications such as Java Virtual Machine runtime error (Virtual MachineError), the class definition error (NoClassDefFoundError) and so on. These errors are not checked because they are outside the control and processing power of the application, but the vast majority are not allowed in the program is running situation. For application design and reasonable, even if an error does occur, in essence, we should not try to deal with the abnormal situation caused by it. In Java, the error described by the subclasses of Error.
Exception is another very important exception classes. It provides exceptions are abnormal program itself can handle. And the error difference is abnormal, the abnormality can be processed, and the error is not handled.

  Exception handling

   1. try ... catch statement processing

   

try 
{ 
   // possible codes abnormality occurs in the try block, once the error, to ensure normal execution 
} the catch (Exception E) {
    // the catch once the program error, it is left, can be used to capture the error information, completion code is not completed within the try block operation 
    e.printStackTrace () // only print error information to the console, when an exception is encountered within the try block jvm automatically calls the current abnormality information and generates an exception object (such as: new ArithmeticException) exception e assigned to him this time we can call his method exception information printout 
} the finally {
     // the finally try ... is a catch block of the structure, with and without the optional, represent the total execution, typically for releasing resources 
}

   2. In addition, the process may not be in a specific location, directly through the throws / the throw thrown in the way, who call this method who handles, main method throws an exception referred JVM virtual machine processing

  

import java.io.*;
public class className
{
  public void deposit(double amount) throws RemoteException
  {
    throw new RemoteException();
  }

  Common exceptions list

  Error program could not handle, internal factors affecting
    java.lang .StackOverflowError stack overflow, computer memory is not enough

  RuntimeException abnormal running
    1. The array index out of bounds (common in the array)
    the java.lang .ArrayIndexOutOfBoundsException
    2. index subscript out of range (common in the collection)
    the java.lang .IndexOutOfBoundsException
    common name in reference 3. The null pointer (null reference) is not abnormal directed to a particular method, the method can not be called
    the java.lang .NullPointerException
    4. The abnormal counts a number by 0
    java.lang.ArithmeticException
    5. The exception type conversion common in type conversion
    java.lang.NumberFormatException

    6.ClassNotFoundException see jar package, you may forget to copy the

    7. com.microsoft.sqlserver.jdbc.SQLServerException jdbc abnormal

Guess you like

Origin www.cnblogs.com/xywl-bky/p/11710618.html