Two ways to handle exceptions in Java

Source: Internet
Author: User

Exception handling in two ways:

    1. The declaration throws the position where the throws declaration is thrown: An exception is thrown up with the throws keyword at the location of the method declaration.

    2. Capture Try....catch.

public class exceptiontest03{public static void Main (string[] args) {//create file input stream read File//think: How does the Java compiler know the following code execution process may   When an exception occurs, how does the//java compiler know that the probability of this exception is high?        The Java compiler is not so intelligent because the FileInputStream method uses the throws FileNotFoundException at the location of the Declaration;            FileInputStream fis=new FileInputStream ("C:/ab.txt"); }}//above program compilation does not pass/*EXCEPTIONTEST03.JAVA:16: Unreported exception java.io.FileNotFoundException; it must be captured or declared in order to throw fileinputstream FIS = New FileInputStream ("C:/ab.txt"); */

Explore throws Keywords

Public class exceptiontest04{public static void main (String[] args)  throws &NBSP;FILENOTFOUNDEXCEPTION{//M1 ()///using throws to handle exceptions is not really handling exceptions but shirking responsibility. Whoever calls will be thrown to whom. The above M1 method if there is an exception, because the use of the upper throw, to JVM,JVM encountered this exception will exit the JVM, the following code will not execute.//SYSTEM.OUT.PRINTLN ("Hello world");// Really deal with TRY{M1 ();} catch (filenotfoundexception e) {}system.out.println ("Hello world");} PUBLIC&NBSP;STATIC&NBSP;VOID&NBSP;M1 ()  throws filenotfoundexception{m2 ();} PUBLIC&NBSP;STATIC&NBSP;VOID&NBSP;M2 ()  throws filenotfoundexception{m3 ();} PUBLIC&NBSP;STATIC&NBSP;VOID&NBSP;M3 ()  throws filenotfoundexception{new fileinputstream ("c:/ Ab.txt ");  //fileinputstream constructs a method declaration position using throws (up)}}/*//a FileNotFoundException type exception occurred during program run.// The JVM has created an object of type FileNotFoundException for us. The following information is carried in the object. The JVM is responsible for printing information about the object to the console. And the JVM stops the program from running. exception in thread  "Main"  java.io.FileNotFoundException: c:\ab.txt  (the system cannot find the file specified.) ) &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSp;at java.io.fileinputstream.open (Native method)         at  java.io.FileInputStream.<init> (fileinputstream.java:106)          at java.io.FileInputStream.<init> (fileinputstream.java:66)       &NBSP;&NBSP;&NBSP;AT&NBSP;EXCEPTIONTEST04.M3 (exceptiontest04.java:31)        &NBSP;&NBSP;AT&NBSP;EXCEPTIONTEST04.M2 (exceptiontest04.java:26)         &NBSP;AT&NBSP;EXCEPTIONTEST04.M1 (exceptiontest04.java:21)         at  exceptiontest04.main (exceptiontest04.java:11) */

Try...catch.

Grammar:

try{

Code that may appear abnormal;

}catch (Exception type 1 variable) {

The code that handles the exception;

}catch (Exception type 2 variable) {

The code that handles the exception;

}....

A 1.catch statement block can write multiple.

2. However, from top to bottom catch, you must catch from the small type exception to the large type exception.

3.try...catch ... A maximum of 1 catch statement blocks are executed in the After the execution is over try...catch ... It's over.

Public class exceptiontest05{public static void main (String[] args) {//The following code compilation fails , because FileNotFoundException did not handle ./*try{//filenotfoundexceptionfileinputstream fis = new  FileInputStream ("C:/ab.txt");} catch (arithmeticexception e) { //caught exception is an arithmetic exception}*///compiled by/*try{//filenotfoundexceptionfileinputstream  fis = new fileinputstream ("C:/ab.txt");} catch (filenotfoundexception e) {}*///The following program compilation fails//Because there are more ioexception not processed./*try{// Filenotfoundexceptionfileinputstream fis = new fileinputstream ("C:/ab.txt"); Fis.read ();} catch (Filenotfoundexception e) { }*///compilation can be done by/*try{//filenotfoundexceptionfileinputstream fis  = new fileinputstream ("C:/ab.txt"); Fis.read ();} catch (filenotfoundexception e) { }catch (ioexception e) {}*///compiled by./*try{fileinputstream fis  = new fileinputstream ("C:/ab.txt"); Fis.read ();} catch (Ioexception e) {}*///compilation cannot write multiple through//catch, but must be from top to bottom, from small to large capture. /*try{fileinputstream fis = new fileinputstream ("C:/ab.txt"); Fis.read ();} catch (ioexception e) {}catch (filenotfoundexception e) {}*/}
public class exceptiontest06{//compilation failed.  //ioexception not processed/*public static void  Main (String[] args)  throws FileNotFoundException{FileInputStream fis = new  FileInputStream ("abc"); Fis.read ();} *///through/*public static void main (String[] args)  throws filenotfoundexception, Ioexception{fileinputstream fis = new fileinputstream ("abc"); Fis.read ();} *///through/*public static void main (String[] args)  throws ioexception{ Fileinputstream fis = new fileinputstream ("abc"); Fis.read ();} */public static void main (String[] args) {try{//program performed an exception that occurred here to the FileNotFoundException type.// The JVM automatically creates an object of type FileNotFoundException that assigns the memory address of the object to the E variable in the CATCH statement block. Fileinputstream fis = new fileinputstream ("abc");//The code above has an exception, the code of the TRY statement block no longer executes, Executes directly into the catch statement block. System.out.println ("ttttttt"); Fis.read ();} catch (filenotfoundexception e) { //e memoryThe address that points to that object in the heap is the "FileNotFoundException type" event. SYSTEM.OUT.PRINTLN ("The read file does not exist!") //filenotfoundexception to override the ToString method in object. System.out.println (e); //java.io.filenotfoundexception: abc  (the system cannot find the file specified.) )}catch (ioexception e) {System.out.println ("Other IO exceptions! ");} System.out.println ("ABC");}}


This article is from the "Gaogaozi" blog, make sure to keep this source http://hangtiangazi.blog.51cto.com/8584103/1661694

Two ways to handle exceptions in Java

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.