Java Exception Handling Mechanism

Source: Internet
Author: User

Java Exception Handling Mechanism

Background:

When using some java class libraries, you will find that when using some class libraries, you must add the try-catch or throw keywords.

Abstract The problem:

Problem Abstraction:

1. The author of the class library can detect the problem that occurs during running, but does not know how to handle the problem.

2. the user who uses this class library knows how to solve this problem, but does not know how to detect them.

Solution:

Exception is used to solve this problem.

1. If a method cannot determine how to handle the problem that occurs in an abnormal process in the program, it can throw the problem to the caller.

2. If the caller is unwilling to handle the problem, he or she can continue to throw it to the next caller, or if he or she is willing to handle the problem, he or she will receive the catch

And then handle the problem.

Java exception hierarchy

 

 

1 Note: The exceptions we are concerned about refer to non-error and runtime exceptions, that is, checkedException (exceptions that can be detected by the compiler)

2 error usually refers to an error that cannot be recovered, such as exceeding the upper limit of system performance. This error is thrown by the system.

3. RuntimeException is caused by a programmer's code problem, such as a null pointer.

 

Specific operations:

In java, checkedException is an object.

Therefore, when an exception is thrown, you must first select an appropriate exception type and then generate an instance to throw

try    {     // code     }   catch (ExceptionType e)    {     // exception handling code     }    

Running process:

When the code encounters an exception, it will ignore the subsequent code and directly jump to its corresponding catch to run

Complete instance:

Exception Handling except 0

public class JavaExceptionTest {    public static void main(String[] args) {        // TODO Auto-generated method stub        try        {            double a=1/0;        }        catch(ArithmeticException e)        {            System.out.println(e.getMessage());        }    }}

Java exception category:

Http://www.tutorialspoint.com/java/java_builtin_exceptions.htm

Java exception Teaching Document

Http://docs.oracle.com/javase/tutorial/essential/exceptions/index.html

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.