J2SE _ Exception Handling

Source: Internet
Author: User

The concept of exceptions

Classification of exceptions

exception capture and handling

ImportJava.io.*; Public classTestEx { Public Static voidMain (string[] args) {Try {            NewTestEx (). F2 (); } Catch(IOException e) {e.printstacktrace (); }                /*int[] arr = {1, 2, 3};        System.out.println (arr[2]);        try {System.out.println (2/0);            } catch (ArithmeticException e) {System.out.println ("system is being maintained, please contact the Administrator");        E.printstacktrace (); }        */                //TestEx te = new TestEx (); //TE.M (0);                /*try {new TestEx (). m (0);            } catch (ArithmeticException ae) {ae.printstacktrace ();        SYSTEM.OUT.PRINTLN ("error"); }        */FileInputStream in=NULL; Try{ in=NewFileInputStream ("MyFile.txt"); intb; b=In.read ();  while(b! =-1) {System.out.print (Char) b); b=In.read (); }    } Catch(IOException e) {System.out.println (E.getmessage ()); } Catch(FileNotFoundException e) {e.printstacktrace (); } finally {        Try{in.close (); } Catch(IOException e) {e.printstacktrace (); }    }         }        voidMintIthrowsArithmeticException {if(i==0)             Throw NewArithmeticException ("Divide by 0"); }        voidF ()throwsFileNotFoundException, IOException {fileinputstream in=NewFileInputStream ("MyFile.txt"); intb; b=In.read ();  while(b! =-1) {System.out.print (Char) b); b=In.read (); }    }        voidF2 ()throwsIOException {/*try {f ();        } catch (FileNotFoundException e) {System.out.println (E.getmessage ());        } catch (IOException e) {e.printstacktrace (); }        */f (); }    }
Testex.java

using a custom exception

declaring and Throwing exceptions

J2SE _ Exception Handling

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.