What is an exception?
An exception is a description of the problem, encapsulating the object of the problem;
Anomaly System features: All classes in the anomaly system and the established objects;
Can be parabolic, that is, can be thrown and throws keyword operation, only the exception system has this feature;
Throws is defined on a function to throw an exception class, and a throw is defined within a function to throw an exception object, which can be thrown multiple, separated by commas.
The code and comments are explained:
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 ("The rectangle parameter appears illegal value");
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 ("The parameter of the circle appears illegal value");//Custom exception, in order to enhance the readability of "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");
}
/*
* exception is a description of the problem, encapsulating the object of the problem
* Characteristics of the anomaly system: all classes in the anomaly system and established objects
* Both have the ability to be parabolic, which means that they can be manipulated by the throw and throws keywords.
* Only abnormal system has this characteristic
* throws is defined on a function to throw an exception class; a throw is defined within a function to throw an exception object, which can throw multiple, separated by commas */
/*
* When the function content has throw throws exception object, does not carry on the try processing, must declare on the function, otherwise the compilation fails
* except Pc:runtimeexcpetion. That is, if the runtimeexcpetion exception is thrown inside the function
* The function can not be declared.
*
* If the function declares an exception, the caller needs to handle it, and the processing method can be throws or try
*
* There are two types of exceptions:
* 1. Compile-time exception
* No throw or try, the compilation fails;
* The exception is identified and the delegate can be processed.
* 2. Runtime exception (not detected at compile time)
* No processing is required at compile time, compiler does not check
* This exception occurs, it is recommended not to handle, let the program stop, need to fix code.
* PC: Multiple catch, the parent's catch is placed at the bottom
* */
Three modes of exception handling
/*1.
try {
Code blocks that need to be instrumented
} catch (Exception e) {
Code blocks that need to be processed
PC: When System.exit (0); At this point the finally code block does not execute, only this case (interview question);
Finally
}finally {
Code blocks that must be executed
Often used to close resources
}
2.
try {
} catch () {
}
3.
try {
} finally {
}*/
/* Custom Exception:
Custom class inherits exception or RuntimeException
1. In order to allow the custom class to have the parabolic nature;
2. Let the class have the common method of operation anomaly;
When you want to customize the exception information, you can use features that are already defined by the parent class.
The exception information is passed to the constructor of the parent class.
Advantages:
1. In accordance with the Java object-oriented thinking, the application of the unique problems to be encapsulated;
2. Separate the normal process code from the problem-handling code for easy reading.
*/
Such as:
Class Novalueexception extends exception{
Novalueexception (String msg) {
Super (MSG);
}
}
Class Novalueexceptiont extends runtimeexception{
Novalueexceptiont (String msg) {
Super (MSG);
}
}
}
An in-depth understanding of object-oriented exception handling the Java exception handling mechanism