Thinking in Java learning Note (v)

Source: Internet
Author: User

1. Java Exception Additions

  A.after catching an exception using Try/catch, the code after the catch will run normally, which is a mistake many novices make.

 Public class exceptiontest {    publicstaticvoid  main (String [] args) {          Try{            thrownew  runtimeexception ();        } Catch (Exception e) {            e.printstacktrace ();        }        System.out.println ("still operate here!" );    }    }

Will output:

Java.lang.RuntimeException
Still operate here!
At exception. Exceptiontest.main (EXCEPTIONTEST.JAVA:20)

  B. When multiple exceptions are caught, once the match exits, the capture exception should be put to the last, otherwise the compilation will not pass

 public  class   Exceptiontest { public  static  void   main (String [] args) {try  { throw  new   RuntimeException ();  catch   (Exception e) {E.printstacktrac        E ();  catch   (RuntimeException e) {e.printst        Acktrace (); } System.out.println ( "still operate here!"    ); }}  

In catch (RuntimeException e) {This line will error:

Unreachable block for runtimeexception.it are handled by the catch block for Exception.

The correct order is to capture the runtimeexception first, and then try to capture exception.

  c. Tells the caller of the calling method that an exception could be thrown, add throws after the method, and then thorw in the method. That way, the exception must be handled or thrown when someone calls the method again.

The following is an example in Bufferedinputstream:

Private byte throws IOException {    byte[] buffer = buf;     if NULL )        thrownew IOException ("Stream closed");         return buffer;    }

It should be noted that the method is followed by throws, the method is not necessarily inside the thorw.

The following is an example in InputStream:

 Public int throws IOException {    return 0;}

  d. When an exception is caught, the subclass's exception can also be captured by the parent class's Try/catch.

 public  class   Exceptiontest { public  static  void   main (String [] args) {try  { throw  new   Subexception ();  catch   (MyException e) {e.printstacktr        Ace (); }}}  class  myexception extends  Span style= "color: #000000;" > exception{}  class  subexception extends  myexception{} 

The output will be:

exception. Subexception
At exception. Exceptiontest.main (exceptiontest.java:15)

Note You can only catch a subclass exception before catching the parent class exception.

  E. Changes the check-time exception to a run-time exception

If the calling method throws an exception, we need to handle it when we call it, and if we don't know what to do with it and don't want to do anything after the catch (which is bad), you can throw an exception up or throw a run-time exception.

 Public classExceptiontest { Public Static voidMain (String [] args)throwsioexception{Try{InputStream is=NewFileInputStream (NewFile ("Abc.txt"));        Is.read (); }Catch(IOException e) {//E.printstacktrace (); //throw e;//throw up when you can't handle it yourself            Throw NewRuntimeException (e); }    }    }

Note that if you throw within a method, you need to capture it again or throws on the method.

  

Thinking in Java learning Note (v)

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.