Exceptions in Java

Source: Internet
Author: User
Tags finally block ming try catch

I. What is an exception?

Java in the run phase of the problem, or error is an exception.

Two. Unusual Inheritance relationships

It can be seen that all exceptions are inherited by Throwable, but in the next layer, they are divided into: Error and exception. Error is mainly in the direction of the JVM, such as insufficient memory, and the request stack depth exceeds the maximum depth. (This is usually the case when you are waiting for death), you only need to consider the exception under exception when designing a Java program.

Three. Write your own exception class

Writing an exception class must inherit from the corresponding exception class, preferably by selecting an exception class that has a similar meaning.

 Packagecom.cjm.exception;/*** My first Exception class * *@authorXiao Ming **/ Public classMyfirstexceptiontext { Public Static voidMain (string[] args)throwsmyfirstexception { for(inti = 0; I < 3; i++) {            if(i = = 2) {                Throw NewMyfirstexception ("You threw out an exception that you wrote yourself.")); }        }    }}classMyfirstexceptionextendsException { Publicmyfirstexception () {} Publicmyfirstexception (String str) {Super(str); }}

Four. Catching exceptions

Form:

try{

Code that may appear to be abnormal

}catch (exception of type 1) {

Processing mode

}

catch (exception of type 2) {

Processing mode

}

 Packagecom.cjm.exception;/*** Use Try catch to catch exceptions * *@authorXiao Ming **/ Public classtrycatchexception { Public Static voidF ()throwsmyfirstexception, mysecoexpection {inti = 1; if(i = = 0) {            Throw NewMyfirstexception ("Catch Your first exception"); } Else {            Throw NewMysecoexpection ("Catch Your second exception"); }    }     Public Static voidMain (string[] args) {Try{f (); } Catch(myfirstexception e) {e.printstacktrace (); } Catch(mysecoexpection e) {e.printstacktrace (); }    }}classMysecoexpectionextendsException { Publicmysecoexpection () {} Publicmysecoexpection (String str) {Super(str); }}

Five. Log

ImportJava.util.logging.Logger;/*** Log * *@authorXiao Ming **/ Public classmydaily { Public Static voidMain (string[] args) {Try {            Throw Newmythrexception (); }Catch(mythrexception e) {System.err.println (e); }                    }}classMythrexceptionextendsexception{Private StaticLogger logger= Logger.getlogger ("My first log");  Publicmythrexception () {StringWriter trace=NewStringWriter (); Printstacktrace (NewPrintWriter (trace));    Logger.severe (Trace.tostring ()); }    }

Six. Finally block

The fianlly statement is bound to be executed, regardless of whether an exception is thrown in the try block.

try{

Code that may appear to be abnormal

}catch (exception of type 1) {

Processing mode

}

catch (exception of type 2) {

Processing mode

}

finally{

Statements that are bound to be executed

}

Example:

 Packagecom.cjm.exception;/*** Finally block * *@authorXiao Ming **/ Public classFinally { Public Static voidMain (string[] args) {Try{System.out.println ("My no exception!" "); }Catch(Exception e) {//Todo:handle ExceptionE.printstacktrace (); }finally{System.out.println ("This step is bound to be executed!" "); }                    }}

 Packagecom.cjm.exception;/*** Finally block * *@authorXiao Ming **/ Public classFinally { Public Static voidMain (string[] args) {Try {            Throw NewMyfirstexception ("with exception thrown"); }Catch(myfirstexception e) {//Todo:handle ExceptionE.printstacktrace (); }finally{System.out.println ("This step is bound to be executed!" "); }    }}

I

Exceptions in Java

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.