Custom exception cases

seday08 Package Penalty for;
/ **
* @author xingsir
* custom exception
* custom exception is often used to define our program, some business logic errors.
* Case: Age illegal abnormal
* /
public class IllegalAgeException the extends Exception {

/ *
* Custom exception need:
* 1: Abnormal defined class name, named to understand, that exception should be done by the class name can roughly determine the reason for what caused this exception
* 2: The need to inherit Exception, at least is its subclasses.
* 3: providing a version number of the sequence (to generate eclipse)
* 4: all constructors (as defined Exception constructor) This can also be generated by the eclipse.
* /
Private static Final Long serialVersionUID = 1L; // Right mouse over IllegalAgeException default Serial Version ID -add
// Click source- point generate constructor from Supperclass, tick Select All
public IllegalAgeException () {
Super ();
// TODO Auto Stub constructor -generated
}

public IllegalAgeException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
// TODO Auto-generated constructor stub
}

public IllegalAgeException(String message, Throwable cause) {
super(message, cause);
// TODO Auto-generated constructor stub
}

public IllegalAgeException(String message) {
super(message);
// TODO Auto-generated constructor stub
}

public IllegalAgeException(Throwable cause) {
super(cause);
// TODO Auto-generated constructor stub
}

 

}

//==================================================================================

seday08 Package;
/ **
* @author xingsir
* using this type of test thrown exception
* /
public class the Person {
Private int Age;

int getAge public () {
return Age;
}
/ *
* @param Age
* @throws Exception
typically used in a method * throw thrown exception, when necessary in the method declaration
* using this declaration throws exception is thrown. Only RuntimeException is an exception.
* /
Public void setAge (int Age) throws IllegalAgeException {
IF (Age <0 || Age> 150) {
/ *
* Use keywords can begin to throw throw an exception
* /
throw new new IllegalAgeException ( "This age is illegal.") ;
}
this.age = Age;
}


}

//=================================================================================

package seday08;
/**
* @author xingsir

* The exception thrown
* /
public class ThrowDemo {

static void main public (String [] args) {
the Person new new P = the Person ();
/ *
* When we call a method throws an exception is thrown statement containing compiler requires us to handle the exception,
* and the processing exception there are two ways:
* 1: use the try-catch catch and handle the exception type thrown
* 2: continue to use throws a statement on the current method throws an exception
* which specific use, combined with the actual business needs.
* /
The try {
p.setAge (1000); // This method is called a method of the class Person, the call is determined
} the catch (Exception E) {
e.printStackTrace (); // error line printing (printing out error line , facilitate positioning)
}
System.out.println (p.getAge ()); // the return value of this time 0
}

}

Guess you like

Origin www.cnblogs.com/xingsir/p/12049558.html