exception handling in Java [Java]-cont.

Source: Internet
Author: User

The exception handler, which consists of a try, catch, finally, and subsequent block. Finally is not a must.

The parentheses of a catch have a parameter that represents the type of exception you want to catch. Catch captures the corresponding type and its derived class. The block following the try contains the action to be taken against the exception type. A try-monitored block may throw more than one type of exception, so an exception handler can have multiple catch modules. Finally, the program block is the program that executes regardless of whether an exception occurs.

ImportJava.io.BufferedReader;Importjava.io.FileNotFoundException;ImportJava.io.FileReader;Importjava.io.IOException; Public classtestioexception { Public Static voidMain (string[] args)throwsIOException {bufferedreader br=NULL; Try{br=NewBufferedReader (NewFileReader ("file.txt")); } Catch(FileNotFoundException E1) {e1.printstacktrace (); }        Try{StringBuilder SB=NewStringBuilder (); String Line=Br.readline ();  while(line!=NULL) {sb.append (line); Sb.append ("\ n"); Line=Br.readline (); } String everything=sb.tostring ();        System.out.println (everything); }Catch(IOException e) {e.printstacktrace (); System.out.println ("IO Problem"); }finally{br.close (); }    }}

exception handling in Java [Java]-cont.

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.