Java Advanced Learning (ii) exception handling

Source: Internet
Author: User
Tags continue exception handling java throws

It's hard to be perfect, and there are all kinds of anomalies. For example, the program itself has bugs, such as when the program prints the printer does not have paper, such as insufficient memory. In order to solve these anomalies, we need to know the cause of the abnormal occurrence. For some common anomalies, we can also provide a certain response plan. The exception handling in C language is simply achieved by the function return value, but the meaning of the return value is often determined by practice. Programmers need to query a lot of information before they can find a vague reason. Object-oriented languages, such as C + +, Java, and Python, often have more complex exception handling mechanisms. This discusses the exception handling mechanism in Java.

Java Exception Handling

Exception handling

A large part of the Java exception handling mechanism comes from C + +. It allows programmers to skip issues that are temporarily out of the way to continue development, or to make the program smarter to handle exceptions.

Java uses special objects to represent exceptional conditions, such that objects are called exception objects. When an exception occurs, Java throws (throw) The object that represents the current condition, depending on the preset settings. The so-called throw is a special way of return. The thread pauses, stepping out of the method call until the exception handler (Exception Handler) is encountered. The exception handler can catch (catch) The exception object and determine the next action based on the object, such as:

Remind users

Handling Exceptions

Continue Program

Exit program

......

The exception handler looks like this, consisting of try, catch, finally, and subsequent blocks of the program. Finally, it is not necessary.

try {   
       
  ...;   
       
}   
       
catch () {   
       
  ...;   
       
}   
       
catch () {   
       
  ...;   
       
}   
       
finally {   
       
  ...;   
       
}

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/Java/

Related Article

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.