This summarizes the Java exception and implements a custom exception class.
Summarize:
- There are two types of Throwable objects: error (for compile-time and system errors), Exception (exceptions thrown in Java class libraries, user methods, and run-time failures).
- exception is subdivided into two types of exceptions: checked exceptions (e.g., IOException, SqlException, etc.). At compile time to be forced to check the exception, need to use try, catch, finally keyword in the compile time processing, otherwise will error. ); Run-time exceptions ( e.g. ,arithmeticexception, classcastexception, IllegalArgumentException, Indexoutofboundsexception, NullPointerException and so on. The compiler does not check for such exceptions. )
- best practices for handling Exceptions in Java programming [4]:
- Use a check-type exception for recoverable errors and a non-check error for programming errors.
- to close or release resources in a finally program block
- include the cause of the exception in the stack trace
- always provide a meaningful complete letter about the exception
- Avoid excessive use of check-type exceptions
- Converting a check exception to a run-time exception
- Remember that it is expensive for performance
- avoid the catch block being empty
- using standard exceptions
- record exceptions thrown by any method
- differences between check exceptions and run-time exceptions:
- checked exceptions need to be handled by themselves and run-time exceptions are not required
- The check exception is a exception direct subclass, and the run-time exception is a subclass of RuntimeException
- Check exception is the programming error, run-time exception is the program run time high failure rate
- best practices for avoiding nullpointerexception [5]:
- call the Equals () and Equalsignorecase () method "Hello" with a known string object. Equals (objstring) ";
- if the return value of the valueof () and ToString () method is the same, use the valueof () method instead of the ToString () method "When the null object calls ToString () Method throws a NullPointerException exception while calling the ValueOf () method returns a "null" wrapper class ";
- use null-safe methods and libraries such as "eg, Stringutils.isblank (), IsNumeric (), iswhitespace (), etc.";
- The return value of a method call is returned with an empty collection or an empty array to avoid the case of NULL, "Collections.empty_list, Collections.empty_set, collections.empty_map, etc.";
- the use of @notnull and @nullable annotations to indicate whether there is a possibility of a null condition;
- Avoid unnecessary auto-boxing and unpacking in your code "to avoid the occurrence of a similar int a with a return value of NULL, an integer a instead of a better";
- abide by the contract and use a reasonable default value "by defining what can be null and what cannot be null, the caller can make a definite judgment." ";
- When you use a database store object, you define whether to allow a field to be empty, so that the database itself mechanism can check whether it is empty, and avoid illegal empty fields after the program call.
- encapsulates an object using NULL as a null class.
To customize a exception class:
The
Projectnameexception.java content is as follows:
Package Com.trianlge23.projectname.exception;public class Projectnameexception extends Throwable {private static final Long serialversionuid = 8093803025939797139l;//exception codeprivate int exceptioncode;//exception detailed Messageprivate string detailmsg;public projectnameexception (int exceptioncode, string extramsg) {super (); This.setdetailmsg (Exceptioncode); this.setextramsg (extramsg);} Public projectnameexception (int exceptioncode) {super (); this.setdetailmsg (Exceptioncode);} Notice:we don't offer the set method to set the excption code.public int GetExceptionCode () {return exceptioncode;} If there have no extra message for this excption code, init it.private void setdetailmsg (int exceptioncode) {This.excepti Oncode = Exceptioncode;if (ProjectNameExceptionCode.EXCEPTION_CODE_MAP.containsKey (Exceptioncode)) {this.detailmsg = ProjectNameExceptionCode.EXCEPTION_CODE_MAP.get (Exceptioncode);} else {this.detailmsg = ProjectNameExceptionCode.EXCEPTION_CODE_MAP.get (projectnameexceptioncodE.projectname_exception_code_not_found);}} If there have extra message for this exception code, add it.private void Setextramsg (String extramsg) {this.detailmsg + = projectnameexceptioncode.extra_exception_msg_spliter+ extramsg;} Override the Super class method GetMessage () @Overridepublic String getMessage () {return this.detailmsg;}}
Projectnameexceptioncode.java content is as follows:
Package Com.triangle23.projectname.exception;import Java.util.hashmap;import Java.util.map;public class Projectnameexceptioncode {//the separator between default message and extra message of exception.public static final Strin G Extra_exception_msg_spliter = ":";//the map stores the EXCEPTION code and EXCEPTION messagepublic static Map<integer , string> exception_code_map;public static final int projectname_exception_code_not_found = 1;static {EXCEPTION_CODE _map = new Hashmap<integer, string> (); Exception_code_map.put (Projectname_exception_code_not_found, "[PROJECTNAME EXCEPTION] not FOUND EXCEPTION CODE.");}}
References:
1. JDK1.7 api:http://docs.oracle.com/javase/7/docs/api/
2. Java Programming Ideas (fourth edition)
3. Effective Java
4. Exception Handling Java Best Practices:
Http://javarevisited.blogspot.com/2013/03/0-exception-handling-best-practices-in-Java-Programming.html
5. Java Tips and best practices to avoid nullpointerexception:
http://javarevisited.blogspot.com/2013/05/ Java-tips-and-best-practices-to-avoid-nullpointerexception-program-application.html
Description:
If you have errors, please correct me, welcome to discuss with us to give guidance.
Last update time: 2014-12-02
Java Custom exception