Java Custom Exception class

Source: Internet
Author: User
Tags getmessage

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

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.