Java language basics: exception Mechanism

Source: Internet
Author: User

1. Throw an exception

  • Throw new
    Throwableclass syntax can throw an exception. throwableclass is a class inherited from throwable, or throwable class itself.

Public void proc () {<br/> // exception is an inherited class of throwable, representing the most basic exception <br/> throw new exception ("hello "); <br/>}< br/> 

  • The code above will cause compilation errors. For methods that throw exceptions, the compiler forces the following: either declare the exception to be thrown on the method (known as the exception description ), you can either use the try block in the method to capture this exception.

// Exception description, indicating the exception to be thrown by this method <br/> Public void proc () throws exception {<br/> throw new exception ("hello "); <br/>}</P> <p> // capture this exception using a try block <br/> Public void proc () {<br/> try {<br/> throw new exception ("hello"); <br/>}catch (exception e) {<br/> E. printstacktrace (); <br/>}< br/>} 

Exception description can be the exception class thrown by the method or the base class of the exception class. For example, throws can be followed by throwable.

  • The method declaration must be included for methods that throw an exception. This is not accurate. When the thrown exception class is runtimeexception or its inherited class, no exception is required:

// The runtimeexception or its inherited class does not require an exception declaration <br/> Public void proc () {<br/> throw new runtimeexception ("hello"); <br/>} 

  • A method can still contain an exception even if it does not throw an internal exception:

Public void proc2 () throws nullpointerexception, filenotfoundexception {<br/>} 

  • If a method has an exception, other methods that call it also have an exception description, or you need to capture the exceptions that may be thrown:

Class A {<br/> Public void proc () throws exception {<br/> throw new exception ("hello "); <br/>}< br/> // exception declaration <br/> Public void proc1 () throws exception {<br/> proc (); <br/>}< br/> // exception declaration. The declared class can be a base class. <br/> Public void proc2 () throws throwable {<br/> proc (); <br/>}< br/> // catch exceptions directly <br/> Public void proc3 () {<br/> try {<br/> proc (); <br/>}catch (exception e) {<br/> E. printstacktrace (); <br/>}< br/>} 

  • Exception classes can be implemented based on exceptions:

Class myexception1 extends exception {<br/>}< br/> class myexception2 extends exception {<br/>}< br/> Class A {<br/> Public void proc () throws myexception1, myexception2 {<br/>}< br/> 

2. Capture exceptions and end cleaning

  • Use the try {} block to enclose the code that may cause exceptions, followed by the exception processor (catch (...) {} block); The Catch Block can have multiple exception classes declared in. When a try block throws an exception, the Execution Code jumps to the Catch Block and starts matching from the first catch block until the matching Catch Block is found. See the following code description:

Class myexception1 extends exception {<br/>}< br/> class myexception2 extends exception {<br/>}< br/> class myexception3 extends exception {<br/>}< br /> Class A {<br/> Public void proc () throws exception {<br/> try {<br/> // used only for demonstration <br/> random = new random (); <br/> int I = random. nextint (3); <br/> if (I = 0) <br/> throw new myexception1 (); <br/> else if (I = 1) <br/> throw new myexcepti On2 (); <br/> else if (I = 2) <br/> throw new myexception3 (); <br/>}< br/> catch (myexception1 E) {<br/> // when myexception1 is thrown, it will jump here. <Br/> E. printstacktrace (); <br/>}< br/> catch (myexception2 e) {<br/> // It is skipped when myexception2 is thrown. <Br/> E. printstacktrace (); <br/>}< br/> catch (exception e) {<br/> // when myexception3 is thrown, because no matching processor exists, <br/> // and exception is the base class of myexception3, so it will jump here. <Br/> E. printstacktrace (); <br/> // you can throw an exception again. The system will look for an exception processor with the outer layer. <br/> throw E; <br/>}</P> <p> public class main {<br/> Public static void main (string [] ARGs) {<br/> A = new A (); <br/> try {<br/>. proc (); <br/>}catch (exception e) {<br/> E. printstacktrace (); <br/>}< br/>} 

  • When a finally clause is added to the exception processor, the finally clause will be called no matter whether the exception occurs or not. The finally clause is often used to clear resources other than garbage collection, such as open files, network Connections:

Class myexception1 extends exception {<br/>}</P> <p> Class A {<br/> Public void proc () {<br/> try {<br/> throw new myexception1 (); <br/>}< br/> catch (myexception1 e) {<br/> E. printstacktrace (); <br/>}< br/> finally {<br/> system. out. println ("hello "); <br/>}</P> <p> public class main {<br/> Public static void main (string [] ARGs) {<br/> A = new A (); <br/>. proc (); <br/>}< br/> the final output is: <br/> myexception1 <br/> at. proc (main. java: 12) <br/> at main. main (main. java: 27) <br/> Hello <br/> 

3. exception restrictions: For an inherited class, if the method covered has an exception description, the exception classes listed must be a subset of the exception classes listed by the method of the base class, let's take a look at an example:

Class myexception1 extends exception {<br/>}< br/> Class A {<br/> Public void proc () {<br/>}< br/> Class B extends a {<br/> // compilation error:. proc has no exception description, so the subclass cannot have an exception description <br/> // The solution is. add exception description to proc: throws myexception1 <br/> // or in throw new myexception1 (); add try block and remove exception description <br/> Public void proc () throws myexception1 {<br/> throw new myexception1 (); <br/>}< br/> 

Let's look at the example:

Class myexception1 extends exception {<br/>}< br/> class myexception2 extends exception {<br/>}< br/> Class A {<br/> Public void proc () throws myexception1 {<br/>}< br/> Class B extends a {<br/> // error: Because. proc only declares the myexception1 exception <br/> Public void proc () throws myexception2 {<br/>}< br/>} 

The constructor is an exception. The inherited class can declare more exception classes, but the exception classes declared by the base class must be added:

Class myexception1 extends exception {<br/>}< br/> class myexception2 extends exception {<br/>}< br/> Class A {<br/> () throws myexception1 {</P> <p >}< br/> Public void proc () throws myexception1 {<br/>}< br/> Class B extends a {<br/> B () throws myexception1, myexception2 {<br/>}< br/> 

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.