The checked and unchecked of Java anomalies

Source: Internet
Author: User

First, Java exceptions are divided into error and exception. Both of these classes are subclasses of interface Throwable. The relationship between error and exception and its subclasses can be broadly described.

Precautions:

1. Error occurs only in Java virtual machines, and users do not need to catch or throw an error in the program.

2. Exception is divided into general exception and runtimeexception two categories. A bit uncomfortable here is that runtimeexception (unchecked) inherits from the parent class of exception (Checked).

Ps:checked and unchecked's concept understanding:

Checked: Generally refers to the program can not directly control the external situation, refers to the compile time need to check the class of exception, the user program must use the try catch mechanism or by throws to the caller to deal with. This type of exception mainly refers to exceptions other than error and runtimeexception and its subclasses.

Unchecked: Refers to a class of exceptions that do not need to be handled at compile time. In the Java system, all the error and runtimeexception and their subclasses are unchecked exceptions. Again, the straightforward understanding of the exception that does not require a mechanism such as a try catch can be considered an unchecked exception.

Checked and unchecked are embodied in the inheritance relationship of Throwable:

+-----------+                 |                 Throwable | +-----------+                  /                          /                     +-------+          +-----------+          |          Error | |          Exception |  +-------+          +-----------+           /  | \           / | \                  \________/       \______/                                                 +------------------+unchecked checked|                                        RuntimeException |    +------------------+                                         /   | |                                              \_________________/unchecked

The following examples illustrate the more awkward points mentioned in note 2 above:

First, define a basic exception class Genericexception, which inherits from exception.

1  Packagecheck_unchecked_exceptions;2 3  Public classGenericexceptionextendsexception{4 5     /**6      * 7      */8     Private Static Final LongSerialversionuid = 2778045265121433720L;9     Ten      Publicgenericexception () { One          A     } -      -      Publicgenericexception (String msg) { the         Super(msg); -     } -}

The following defines a test class verifyexception.

1  Packagecheck_unchecked_exceptions;2 3  Public classverifyexception {4 5      Public voidFirst ()throwsgenericexception {6         Throw NewGenericexception ("Checked exception");7     }8     9      Public voidsecond (String msg) {Ten         if(msg = =NULL){ One             Throw NewNullPointerException ("Unchecked exception"); A         } -     } -      the      Public voidThird ()throwsgenericexception{ - First (); -     } -      +      Public Static voidMain (string[] args) { -Verifyexception ve =Newverifyexception (); +          A         Try { at Ve.first (); -}Catch(genericexception e) { - e.printstacktrace (); -         } -  -Ve.second (NULL); in     } -  to}

After running, get the following information on the Eclipse console:

 1  check_unchecked_exceptions. Genericexception:checked exception  2  at Check_unchecked_exceptions. Verifyexception.first (Verifyexception.java:6)  3  at Check_unchecked_exceptions. Verifyexception.main (Verifyexception.java:23)  4  Exception in thread "main"  5  at Check_ Unchecked_exceptions. Verifyexception.second (Verifyexception.java:11)  6  at Check_unchecked_exceptions. Verifyexception.main (verifyexception.java:29) 

The above example, combined with the concept of checked and unchecked, shows that exception is the checked type, but its subclasses runtimeexception (Subclass NullPointerException) is unchecked.

The checked and unchecked concepts described in this blog post are very basic, but we do not necessarily understand ...

The checked and unchecked of Java anomalies

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.