Java Foundation----The use of exceptions in >java (i)

Source: Internet
Author: User

Today we'll take a general look at the knowledge of exceptions in Java.

Java exception Usage--Some explanations of Java exceptions
1 , the Throwable class is a superclass of all errors or exceptions in the Java language (this is what all can throw). It has two sub-classes: Error and exception.  2. Exception: It points out the conditions that a reasonable application wants to capture. Exception is divided into two categories: one is checkedexception, the other is uncheckedexception. The difference between the two types of exception is mainly checkedexception need to use try ... Catch... The capture is displayed, while the uncheckedexception does not need to be captured. Usually uncheckedexception is also called RuntimeException.

Second, some code to understand the exception
    • IllegalArgumentException is an exception to uncheckedexception.
 Public Static int divide (intint  num2) {    if (num2 = = 0)        {throw  New//  This exception is not Try...catch     }    System.out.println (" Hello world ");     return NUM1/ num2;}

The effect of the operation is as follows:

Exception in thread "main"  java.lang.IllegalArgumentException: The divisor cannot be 0    At Com.chenhui.huhx.ExceptionTest.divide (Exceptiontest.java:one) at    com.chenhui.huhx.ExceptionTest.main(exceptiontest.java:37 )
    • indexoutofboundsexception is an exception to uncheckedexception.
 Public Static void arraysoutbounds () {    intnewint[]{2, 3, 5};     Try {        System.out.println (arrays[4]);     Catch (indexoutofboundsexception e) {        System.out.println ("Array out of Bounds error");    }    System.out.println ("execution after array exception");}

The effect of the operation is as follows:

Array out of bounds error after array exception execution
    • FileNotFoundException is an anomaly of checkedexception, which must try...catch or throw.
 Public Static void checkexception () {    try  {        thrownew filenotfoundexception (" File not Found ");     Catch (FileNotFoundException e) {        e.printstacktrace ();    }    System.out.println ("Execution after file exception");}

The effect of the operation is as follows:

java.io.FileNotFoundException: File not found    At com.chenhui.huhx.ExceptionTest.checkException (Exceptiontest.java:) at    com.chenhui.huhx.ExceptionTest.main(Exceptiontest.java:39 ) execution after file exception

The entire test file is as follows:

1  PackageCom.chenhui.huhx;2 3 Importjava.io.FileNotFoundException;4 5 /**6 * Created by Huhx on 2017-04-27.7  */8  Public classExceptiontest {9      Public Static intDivideintNUM1,intnum2) {Ten         if(num2 = = 0) { One             Throw NewIllegalArgumentException ("Divisor cannot be 0");//This anomaly is not try...catch. A         } -System.out.println ("Hello World"); -         returnNUM1/num2; the     } -  -      Public Static voidarraysoutbounds () { -         int[] Arrays =New int[]{2, 3, 5}; +         Try { -System.out.println (arrays[4]); +}Catch(indexoutofboundsexception e) { ASYSTEM.OUT.PRINTLN ("Array Out of bounds error"); at         } -System.out.println ("Execution after array exception"); -     } -  -      Public Static voidcheckexception () { -         Try { in             Throw NewFileNotFoundException ("File not Found"); -}Catch(FileNotFoundException e) { to e.printstacktrace (); +         } -System.out.println ("Execution after file exception"); the     } *  $      Public Static voidMain (string[] args) {Panax Notoginseng //Divide (3, 0); - //arraysoutbounds (); the checkexception (); +     } A}
exception test Class

Friendship Link
    • Intellj idea2017 's Crack method: http://quanzhan.applemei.com/webStack/T1RnMw==

Java Foundation----The use of exceptions in >java (i)

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.