Object-oriented Exception Handling: an in-depth understanding of java exception handling mechanism, and an in-depth understanding of Exception Handling

Source: Internet
Author: User

Object-oriented Exception Handling: an in-depth understanding of java exception handling mechanism, and an in-depth understanding of Exception Handling

What is an exception?

An exception is a description of the problem. It encapsulates the problem object;
Characteristics of the exception system: all classes and objects in the exception system;
Can be throttled, that is, it can be operated by the throw and throws keywords, only the exception system has this characteristic;
Throws are defined on functions and used to throw exception classes. throw is defined on the function and used to throw exception objects. The former can throw multiple objects, separated by commas.

Code and comments:

Package demo1;
/*
* There is a circle and a rectangle *
**/
Class NoValueException extends Exception {
NoValueException (String msg ){
Super (msg );
}
}
Class NoValueExceptionT extends RuntimeException {
NoValueExceptionT (String msg ){
Super (msg );
}
}
Interface Shape {
Void getArea ();
}
Class Rec implements Shape
{
Private int len, wid;
Rec (int len, int wid) // throws NoValueException
{
If (len <= 0 | wid <= 0)
Throw new NoValueExceptionT ("invalid value of the rectangle parameter ");
This. len = len;
This. wid = wid;
}
Public void getArea (){
System. out. println (len * wid );
}
}

Class Circle implements Shape {

Private int r;
Circle (int r ){
If (r <= 0)
Throw new RuntimeException ("invalid circle parameter value"); // custom exception, to enhance readability "NoValueExceptionT"
This. r = r;
}
Public static final double PI = 3.14;

Public void getArea (){
System. out. println (Math. pow (r, 2) * PI );
}

}

Public class test1 {

Public static void main (String [] args ){
/* Try {
Rec rec = new Rec (-4, 5 );
Rec. getArea ();
} Catch (NoValueException e ){
System. out. println (e. toString ());
System. out. println (e. getMessage ());
E. printStackTrace ();
}*/


/* Rec rec = new Rec (4, 5 );
Rec. getArea ();*/
Circle c = new Circle (-8 );
C. getArea ();
System. out. println ("over ");

}
/*
* An exception is a description of the problem. It encapsulates the problem object.
* Characteristics of the exception system: all classes and objects in the exception system
* Both are throttled, that is, they can be operated by the throw and throws keywords.
* Only the exception system has this feature
* Throws are defined on functions and used to throw exception classes. throw is defined on the function and used to throw exception objects. The former can throw multiple objects separated by commas */

/*
* When throw throws an exception object in the function content without try processing, it must be declared on the function; otherwise, compilation fails.
* Pc: Except runtimeexcpetion. That is, if the runtimeexcpetion exception is thrown in the function
* You do not need to declare the function.
*
* If the function declares an exception, the caller must handle it. The processing method can be throws or try.
*
* There are two types of exceptions:
* 1. Exception during compilation
* If no throw or try is thrown, the compilation fails;
* The exception is identified, indicating that the exception can be handled.
* 2. runtime exception (not detected during compilation)
* No processing is required during compilation, and the compiler does not check
* If this exception occurs, we recommend that you do not handle it and stop the program. You need to modify the code.
* Pc: Multiple catch. The catch of the parent class is placed at the bottom.
**/


// Three exception handling modes
/* 1.
Try {
Code block to be detected
} Catch (Exception e ){
Code block to be processed
Pc: When System. exit (0); at this time, the finally code block is not executed, only this situation (interview questions );
Finally
} Finally {
Required code block
Used to close Resources
}
2.
Try {

} Catch (){

}

3.
Try {

} Finally {

}*/

/* Custom exception:
Custom class inherits Exception or runtimeException
1. To make the custom class disposable;
2. Let this class have a common method for abnormal operations;
To customize the exception information, you can use the features defined by the parent class.
Pass exception information to the constructor of the parent class.
Advantages:
1. encapsulate special problems in the program according to java's Object-oriented thinking;
2. Separate the normal process code from the problem handling code for easy reading.
*/
// For example:
Class NoValueException extends Exception {
NoValueException (String msg ){
Super (msg );
}
}
Class NoValueExceptionT extends RuntimeException {
NoValueExceptionT (String msg ){
Super (msg );
}
}

}

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.