Java Exception handling 2

Source: Internet
Author: User

 Package Lianxi3;
Exception handling method Two: When this method has an exception, throws an object of the exception class, throwing the call to the method
//person. the object of the exception can be thrown up and down, until main, in the process of throwing, can also be used Try-catchImportJava.io.File;ImportJava.io.FileInputStream;Importjava.io.FileNotFoundException;Importjava.io.IOException; Public classTestException { Public Static voidMain (string[] args) {Try{method2 (); }Catch(FileNotFoundException e) {System.out.println (E.getmessage ()); }Catch(IOException e) {e.printstacktrace (); }} Public Static voidMETHOD2 ()throwsfilenotfoundexception,ioexception{method1 ();} Public Static voidMethod1 ()throwsfilenotfoundexception,ioexception{FileInputStream fis=NewFileInputStream (NewFile ("Hello.txt")); intstr;  while((Str=fis.read ())!=-1) {System.out.print (Char) str);   } fis.close ();} }

Exception handling three: manually throwing exceptions

The type of exception thrown, if RuntimeException can not be displayed for processing.

If a exception, it must be handled explicitly. such as throw new RuntimeException ("The incoming type is wrong!") ”);

Java Exception handling 2

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.