Java Note 9__ exception/throw keyword

Source: Internet
Author: User

/*** Exception: Some instructions that cause the program to run in a program * 1. Exception: Compile period * 2. Non-inspected exception: Runtime * Exception Handling Process Analysis: * 1. Once an exception is generated, the system automatically generates an instantiated object of the exception class * 2. If a corresponding Try statement is present, execute, or Exit, and the system reports an error **/ Public classMain { Public Static voidMain (string[] args) {/*try{//}catch (Exception type Object) {//exception-handling Operation}catch (Exception type Object) with potentially unexpected exception Exception processing Operation} ... finally{//exception of the unified export}*/        intA = 100; Scanner input=NewScanner (system.in); Try{             for(inti=0;i<10;++i) {                intb =Input.nextint (); intc = A/b;            System.out.println (c); }        }Catch(ArithmeticException e) {System.out.println ("Arithmetic Operation exception"); E.printstacktrace (); //exception information in the output stack}Catch(Exception e) {System.out.println ("Operation Exception"); }finally{//Purpose: Release of resourcesSystem.out.println ("Finally: execution occurs regardless of whether the statement in the try is an exception"); } div (10,2); }         Public Static intDivintAintb) {        intRes=0; Try{res=a/b; returnRes//execute this sentence when you're done with the finally thing .}Catch(ArithmeticException e) {e.printstacktrace (); return0; }finally{System.out.println ("End of division operation"); }    }}

 Public classMain { Public Static intAdd ()throwsinputmismatchexception{//throw, let the superior to deal withScanner input =NewScanner (system.in); Try{            intA =Input.nextint (); intb =Input.nextint (); returnA +C; }Catch(inputmismatchexception e) {//throw new Inputmismatchexception ("There is a mistake.");            Throwe; }finally{System.out.println ("All finished."); }    }     Public Static voidMain (string[] args) {Try{System.out.println (Add ()); }Catch(Inputmismatchexception e) {//superiors to deal withE.printstacktrace (); }    }}

Java Note 9__ exception/throw keyword

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.