Java Exception Handling and comparison with C ++ -- Exception classification. Test exceptions. Non-test exceptions. Common exceptions

Source: Internet
Author: User

Like C ++, Java also has some keywords for exception handling. Note: The following two examples only aim to use all keywords as much as possible. To introduce the location of keywords, we do not recommend the following practices. Keyword for exception handling in C ++: try. catch. throw, basic usage example: [cpp] try {throw E;} catch (E e) {} catch (...) {} try {throw E;} catch (E e) {} catch (...) {} throw E throws a defined exception and then catches the exception. Catch (...) is used to handle all exceptions that are not caught above. Keyword for exception handling in Java: try. catch. finally. throw. throws, basic usage example: [java] public void Err () throws E {try {...} catch (E e) {throw e;} finally {...}} public void Err () throws E {try {...} catch (E e) {throw e;} finally {...}} try. catch is similar to C ++, but the catch here does not catch (...), however, there is a finally block, which is a block that will be executed no matter whether there is an exception or whether the exception is captured after the try. In addition, throw is used to throw an exception, and throws is used to explain what exceptions a function may throw. Java Exception classification Throwable -- Exception base class Error -- Throwable subclass, which is usually thrown by a virtual machine. A serious Error occurs. What the program wants to do is to exit safely, and others cannot do anything about Exception -- Throwable subclass, it is generally a Child class of RuntimeException -- Exception caused by a program, an Exception that occurs during running, an array out of bounds, and a null pointer, generally, you can modify the program to avoid non-RuntimeException -- Exception subclasses. IOException and other test exceptions and non-test exceptions. In some cases, these two concepts are very difficult to understand, my personal understanding is: 1. the check exception means that the compiler will check whether you catch the exception. The compiler will force you to write the corresponding catch statement. Otherwise, an error is reported during compilation. These exceptions are generally followed by throws. That is to say, the methods indicate that they may throw an exception. As for the reason why the compiler should do this, it is easy to understand, because I have a method, since I have foreseen that exceptions may be thrown, you have to deal with it, I remind you that something will go wrong. If you don't handle it, what is your concern? All exceptions except the test exceptions belong to this category. 2. A non-checksum exception means that the compiler will not check whether you catch the exception, for example, if the array is out of bounds or a null pointer, the compiler will not take care of you. RuntimeException and Error belong to this type. The above is my understanding. If there are any mistakes, please point them out so that you can correct them in time. Thank you! Common exception cause -- array subscript out-of-bounds ClassCastException ----------- forced conversion failed cause ----- parameter exception cause ----------- NULL pointer exception AssertionError --------------- asserted error predictionininitializererror -- initialization exception StackOverflowError ----------- Stack Overflow

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.