The difference between checked exception and unchecked exception in Java

Source: Internet
Author: User

Java defines two types of exceptions:

- Checked exception: Inherit from Exception class is Checked exception. The code needs to handle the checked exception that the API throws, either with a catch statement or directly with a throws statement.

- Unchecked exception: Also known as runtimeexception, it is also inherited from exception. But all runtimeexception subclasses have a feature, that is, the code does not need to handle their exceptions can also be compiled, so they are called unchecked exception. RuntimeException (Run-time exception) does not require try...catch ... or throws mechanism to handle the exception.

The inheritance level of the nullpointerexception.

NullPointerException inherits from RuntimeException, so it is a unchecked exception.

The most commonly used five kinds of runtimeexception:

ArithmeticException

int a=0;
int b= 3/a;

ClassCastException:

Object x = new Integer (0);
System.out.println ((String) x);

Indexoutofboundsexception
ArrayIndexOutOfBoundsException,
Stringindexoutofboundsexception

int [] numbers = {1, 2, 3};
int sum = numbers[3];

IllegalArgumentException
NumberFormatException

int a = Interger.parseint ("test");

Nullpointerexceptionextends

Summary:

Check exception: Do not handle compilation cannot pass

Non-inspection exception: Do not handle compilation can pass if there is thrown directly to the console.

Run-time exception: is a non-check exception

Non-run-time exception: is an inspection exception

The difference between checked exception and unchecked exception in 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.