Java Basics-Exception--handling of multiple exceptions

Source: Internet
Author: User

Handling of multiple exceptions

1, when declaring an exception, it is recommended to declare more specific exceptions, so that the treatment can be more targeted

2, the other side of the declaration of a few exceptions, you should have a few catch blocks, do not define redundant catch blocks

If an inheritance relationship occurs in multiple catch blocks, the parent exception catch block is placed at the bottom.

Attention:

/* When you make catch processing, you must define the specific handling in the catch.

Do not simply define a sentence e.printstacktrace (),----because the user cannot read

Also do not simply write an output statement */

There is a problem, in the catch inside the set up, log the problem to the log file for maintenance personnel to view.

--------------------------

Exceptiontest.java

 Public classExceptiontest { Public Static voidMain (string[] args) {Divdemo dd=NewDivdemo (); intResultd; Try{Resultd= Dd.div (10, 0); System.out.println ("Resultd ' value is:" +Resultd); } Catch(ArithmeticException e) {e.printstacktrace (); } Catch(arrayindexoutofboundsexception e) {e.printstacktrace (); } Catch(Exception e) {e.printstacktrace (); } System.out.println ("Over!"); }}classdivdemo{ Public intDivintAintbthrowsarithmeticexception,arrayindexoutofboundsexception {int[] arr =New int[5];        System.out.println (Arr[a]); returnA/b; }}

: Console:

Java Basics-Exception--handling of multiple exceptions

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.