Difference between Exception and RuntimeException in Java

Source: Internet
Author: User

In java's exception class system, Error and RuntimeException are non-checked exceptions, while others are checked exceptions ).
Non-checktype exception, that is, in the program, you do not need to try .. catch .. check type exception, you must use try .. catch, or use throws to continue the transfer in the method definition.
All methods can throw RuntimeException and its subclass without declaring throws, but they cannot throw non-RuntimeException without declaring throws.

For non-RuntimeException, you must write catch blocks for processing.

Read a piece of code

public class MyDemo {    public static void main(String[] args) {        // TODO Auto-generated method stub        String str="123";        int temp=Integer.parseInt(str);        System.out.println(temp*temp);    }}

Let's take a look at the source code of the parseInt method as follows:

 

   public static int parseInt(String s) throws NumberFormatException {    return parseInt(s,10);   }

We found that the NumberFormatException was thrown in this method, but we didn't find try... catch in the above Code to handle it. Why. According to our knowledge about exception handling, if an exception is thrown through throws, try... catch, but there must be try... catch (or throws to throw ).

This is because NumberFormatException is an unchecked exception. It is a non-checked exception and does not need to be processed. Although we can capture Possible exceptions through try... catch, this is not necessary.

This can be seen through the inheritance relationship of the class.

We can find that NumberFormatException is a subclass of RuntimeException, so we need to understand the concepts of Exception and RuntimeException:

1. Exception: try... catch must be used in the program for processing.

2. runtimeexception: Do not use try... catch for processing. However, if an exception is generated, the exception will be handled by JVM.

The exception handling mechanism is also recommended for subclasses of runtimeexception. Although the exception of runtimeexception does not support try... if an exception occurs, the program will be interrupted. Therefore, to ensure that the program can still be executed after an error occurs, try... catch exception handling mechanism.

Below is a list of common runtimeexception:

1. nullpointerexception: NULL pointer

2. numberformatexception: The number format is incorrect. It is inherited from illegalargumentexception and occurs when the string is converted to a number.

3. arrayindexoutofboundsexception: array out of bounds

4. stringindexoutofboundsexception: the string is out of bounds.

5. classcastexception: type conversion error.

6. unsupportedoperationexception: this operation is not supported.

7. arithmeticexception: arithmetic error.

8. illegalargumentexception: Invalid parameter.

 

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.