Summarize Java exception handling-handling Exceptions

Source: Internet
Author: User

    1. The exception in Java is not the error or exception that is described in the real world, but rather a branch that distinguishes it from normal processes. Also become an exception. When an exception occurs, a so-called exception handler catches the thrown exception and then does the exception.


    1. The exception in Java, and the exception in our reality, is not meant to be, he is another branch of the program run, also known as an exception.

    2. Let's look at the structure of the anomaly.

try{//Normal process, may throw exception//normal business code} catch (Exception type name) {//Another branch, exception handling logic} finally {//the area must be gone (optional)}

The above structure is the most common structure of our programming, of course, the finally statement block can be omitted, according to the appropriate use of the business finally can play a good role.

3. Types of exceptions

Divided into two kinds of 1.Checked Exception, 2.Runtime Exception

For 1, the error is an expected exception, which is the kind of exception that can be detected during program compilation, and of course allows you to fix such exceptions.

For 2, an exception occurs during program execution and does not necessarily expect it to occur, and processing is not required during compilation. If such an exception is not handled, it is thrown out until it is thrown to the JVM for processing. The log of unusual information we see most often is the result of JVM processing.

4. Countermeasures

For these two types of exceptions, let's talk about what to do.

For the first, we can anticipate the anomalies that occur, and of course the Eclipse software will also prompt for exception information, and we'll build another branch, of course, catch statement block.

For the second exception we have to deal with the following two noun throw,throws.

The use of 5.throw,throws

Throw, you want to throw an exception yourself and generate the specified exception type (custom exception). This is a wise choice when you are not sure that you want to handle the exception completely, or if you let the caller handle it yourself, and throw the exception back in the catch.

Throws: Method exception list declaration, this method has the possibility of an exception, let the caller handle it by itself.


Important: When choosing an exception handling method, if you know what to do on the current node, use Try-catch, if you do not know how to handle it, use throws to give the caller to handle it. If you can only complete part of the processing, first try-catch,throw the exception, with throws for the upper-level caller to continue processing.


6. Say the custom exception, usually, is inherited exception type, in the run time, to inherit RuntimeException

Be careful to overload the constructor as well.

This article may also serve as the interview reference content, if everybody has any question, may express your opinion, thanks.


This article is from a "snowball" blog, be sure to keep this source http://gunxueqiu.blog.51cto.com/7229538/1571876

Summarize Java exception handling-handling Exceptions

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.