Java Exception Handling

Source: Internet
Author: User

Background introduction:

In the use of some Java libraries, you will find that some library use must add try-catch or throw keyword.

Abstract describes the problem:

Problem Abstraction:

1 class repository authors can detect problems in the run, but do not know how to handle the problem.

2 Using this class repository users know how to deal with this problem, but do not know how to detect them

Solve:

The effect of an exception (exception) is to solve this problem.

1 If a method does not determine how to handle the problem of non-normal processes in the program, then it can throw the problem (throw) to the caller to handle.

2 If the caller is unwilling to process then it can continue to throw (throw) to the next caller, or if willing to handle the problem then receive (catch) this

Problem, and then proceed with processing.

Java Exception hierarchy

1 Note: The exceptions we are concerned with are non-error and runtime exceptions, i.e. checkedexception (exceptions that can be detected by the compiler)

2 error usually refers to unrecoverable errors, such as exceeding the upper bounds of system performance. This error is thrown by the system.

3 RuntimeException is a problem with programmer code such as null pointers

Specific operation:

The checkedexception in Java is an object.

So when you throw an exception, you need to first select the appropriate exception type, and then the generated instance throws

Try     {     //    }   catch  (Exceptiontype e)    {      // Exception handling code     }    

Run the process:

When the code encounters an exception, it ignores the code behind it and jumps directly to its corresponding catch.

A complete example:

Except 0 exception handling

 Public class javaexceptiontest {    publicstaticvoid  main (string[] args) {         // TODO auto-generated Method stub        Try         {            double a=1/0;        }         Catch (arithmeticexception e)        {            System.out.println (e.getmessage ());     }}}

Exception categories for Java:

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

Java Exception Teaching Documentation

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

Java Exception Handling

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.