Exception Handling in Java

Source: Internet
Author: User

How the JVM handles exceptions by default

When the main function receives this problem, there are two ways to handle it:

    1. Handle the problem yourself, and then continue to run
    2. There is no way to deal with it, only the JVM that calls main to handle
      The JVM has a default exception handling mechanism that handles the exception. and the name of the exception, the exception information. The location where the exception occurred is printed on the console, and the program stops running
Exception handling Mode try...catch...finally

Try: Used to detect exceptions
Catch: Used to catch exceptions
Finally: freeing resources

P style= "margin:0px 0px 1.2em!important" > When the problem is handled by Trycatch, the program will continue to execute

  1. try catch
 //notation 1 int a = 10;        int b = 0;        Int[] arr = {11,22,33,44,55};            try {System.out.println (A/b);            System.out.println (arr[10]);            arr = null;        System.out.println (Arr[0]);        } catch (ArithmeticException e) {System.out.println ("divisor cannot be zero");        } catch (ArrayIndexOutOfBoundsException e) {System.out.println ("Index Out of Bounds");            } catch (Exception e) {//exception e = new NullPointerException ();        SYSTEM.OUT.PRINTLN ("error"); }
//写法2//JDK7如何处理多个异常        try {            System.out.println(a / b);            System.out.println(arr[10]);        } catch (ArithmeticException | ArrayIndexOutOfBoundsException e) {            System.out.println("出错了");        }

Multiple catch, can only catch an exception, try after if with more than one catch, then small exception put in front, large exception put behind, according to the principle of polymorphism, if the large put in front, will all the sub-class objects receive, after the catch is meaningless

  1. Try Catch finally
  2. Try finally
The difference between throws and throw throws
    1. Used after the method declaration, followed by the exception class name
    2. Can be separated by a comma with multiple exception class names
    3. Represents a thrown exception that is handled by the caller of the method
Throw
    1. Used in the body of a method, followed by the name of the exception object
    2. Only one exception object name can be thrown
    3. Represents a thrown exception, handled by a statement in the method body
Features of finally keyword-specific and function-finally

The statement body that is finally controlled must be in the try Catch Finally in the execution.

Special case: The JVM exits before execution to finally (e.g. System.exit (0))

The role of finally

Used to free up resources, and in IO stream operations and database operations, you will see

The difference between final,finally and finalize: The relationship between Lei Feng and Leifeng Tower

Final can be decorated with classes, cannot be inherited, decorated methods, cannot be overridden, modifier variables, can only be assigned one time
Finally is a statement body in a try statement and cannot be used alone to free resources
Finalize is a method that is called by the object's garbage collector when the garbage collector determines that no more references to the object exist.

Custom exceptions

Why do I need a custom exception?
By the name of the end is the god horse anomaly, there are targeted solutions, such as: The Age of people

class AgeOutOfBoundsException extends Exception {    public AgeOutOfBoundsException() {        super();    }    public AgeOutOfBoundsException(String message) {        super(message);    }}

Exception considerations:

  1. When a subclass overrides a parent class method, the child class's method must throw the same exception or subclass of the parent exception. (father is broken, son can not be worse than father)
  2. If the parent class throws more than one exception, when the subclass overrides the parent class, it can only throw the same exception or a subset of his, the subclass cannot throw exceptions that the parent class does not have
  3. If the overridden method does not throw an exception, then the method of the subclass is absolutely not allowed to throw an exception, and if an exception occurs within the subclass method, then the subclass can only try, not throws
?

Exception Handling in Java

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.