Two ways to catch exceptions exception

Source: Internet
Author: User
Tags throwable

1. Throw an exception: Let the code that calls this method go to the Tube

public static void GetFile () throws exception{}

Package Com.throwable;import Java.io.bufferedreader;import Java.io.file;import java.io.fileinputstream;import Java.io.filenotfoundexception;import Java.io.inputstreamreader;public class Excetiondemo {/** * @param args * @throws Exception */public static voidMain (STring[] args) {//TODO auto-generated method stubtry {GetFile ();}Catch(Exception e)            {//TODO auto-generated catch Blocke.printstacktrace ();} }Public static void GetFile () throws Exception{File File = new file ("C:\\test.java");         FileInputStream fis = new FileInputStream (file);         BufferedReader br = new BufferedReader (new InputStreamReader (FIS)); Unhandled exception type FileNotFoundException//2 quick fixed available:add throwable;         Surrownd try/catch String line = Br.readline ();         System.out.println (line); Output result: Heheheheh}}

  

2. Around the code block that is currently going to appear the exception, try--catch

Package Com.throwable;import Java.io.bufferedreader;import Java.io.file;import java.io.fileinputstream;import Java.io.filenotfoundexception;import Java.io.ioexception;import Java.io.inputstreamreader;public Class ExcetionDemo2 {/** * @param args * @throws Exception */public static void Main (string[] args) {//TODO auto-generated met Hod stubgetfile ();}         public static void GetFile () {File File = new file ("C:\\test.java"); FileInputStream fis;try {FIS = newfileinputstream (file);BufferedReader br = new BufferedReader (new InputStreamReader (FIS)); Unhandled exception type FILENOTFOUNDEXCEPTION//2 quick fixed available:add throwable; Surrownd try/catch String line;line =br.readline ();System.out.println (line); Output result: Heheheheh}catch (Exception e) {TODO auto-generated catch Blocke.printstacktrace ();} }}

  

Two ways to catch exceptions exception

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.