Classes 1:public class Logicexception extends RuntimeException {
Business logic Exceptions
/**
*
* @param message Exception information
*/
Public Logicexception (String Message) {
Super (message);
}
/**
*
* @param message Exception information
* @param cause the root cause of the current exception
*/
Public logicexception (String message, throwable Cause) {
Super (message, cause);
}
}
Class 2:private static string[] Arr={"zhang san", "xiao zhang", "xiao ming", "john doe"};
public static void main (string[] Args) {
try {
Checkusername ("zhang san");
} catch (logicexception E) {
String Errormsg=e.getmessage ();
System.out.println ("show users" +errormsg);
}
}
private static Boolean Checkusername (String Username) {
For (String Name:arr) {
If (name.equals (username)) {
throw new Logicexception ("sorry," +username+ "is already registered!) ");
}
}
System.out.println ("registered successfully! ");
Return true;
}
}
Summary 1. Custom Exception: class Exception class name extends Exception {public Exception class name (String Msg) {super (msg); }} 2. Identify the exceptions that may be thrown: throws exception class name 1, exception class Name 2 3. Catch exception: try{} catch (exception class name Y) {} catch (exception class name Y) {} 4. method to interpret GetMessage ()//output exception information print StackTrace ()//output result in more detailed information
Java Custom Exception class