Java basics: exception capture sequence, java capture sequence

Source: Internet
Author: User

Java basics: exception capture sequence, java capture sequence

Reprinted with the source: jiq •'s technical Blog

Public voidtestException () {int a [] = {1, 2, 3}; int q = 0; try {for (int I = 0; I <. length; I ++) {a [I]/= q ;}} catch (ArithmeticException h) {System. out. print ("ArithmeticException \ n"); // execute} catch (Exception e) {System. out. print ("Exception \ n"); // no execution, the Exception must be placed after the ArithmeticException/*** with a wider range not only must be put behind the Exception *, but will not be executed (blocked by the earlier range * with fewer exceptions ), so what's the significance ??? */} Finally {System. out. print ("finally \ n") ;}// <span style = "color: # 3333ff;"> output </span> ArithmeticExceptionfinally

* Point 1: Although ArithmeticException inherits from Exception, an ArithmeticException occurs.

* When the Exception is captured, only the Exception that actually occurred will be captured, not because the Exception is its parent class.

* Execute the catch clause of Exception.

* Point 2: however, if you try to put the catch statement with a wider range of exceptions in front of the catch statement

* The error "Unreachablecatch block for ArithmeticException.

* Itis already handled by the catch block for Exception"

* An exception with a larger scope (parent class) must be placed behind it. If there is no inheritance relationship, such as ClassNotFoundException,

* There is no sequential relationship between catch clauses of ArithmeticException.

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.