Java--the capture and processing of exceptions--Exception class and RuntimeException class

Source: Internet
Author: User

7.3 Exception class and RuntimeException class

The relation and difference between exception class and RuntimeException class???

Example: A string becomes an integral type

Class:runtimeexceptiondemo01

 Package Limethrowable._7_3;  Public class RuntimeExceptionDemo01 {    publicstaticvoid  main (string[] args) {        = "123";         int i = integer.parseint (str);         * i);}    }

Console:

15129

where parseint () signature:

 Public Static int throws NumberFormatException

The parseint () method uses the throws keyword at the time of declaration, but does not use a try---catch when the method call is processed, why?

Observe the inheritance relationship of the NumberFormatException class:

    

It can be found that numberformatexception belongs to RuntimeException subclasses, then it is clear to know:

⊙exception must be handled in the program using a try---catch.

⊙runtimeexception can be handled without practical try---catch, but if there is an exception, the exception is handled by the JVM.

Tips:

For RuntimeException subclasses it is best to also use exception handling mechanisms.

Although the runtimeexception exception can be handled without a try---catch, it is sure to cause the program to break if an exception occurs, so that, in order to ensure that the program can still be executed after an error, Therefore, it is best to use the try---catch exception handling mechanism when developing code.

7.4 Custom Exception classes

La La la

Java--Exception capture and processing--Exception class and RuntimeException class

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.