Since graduation, I have worked in several project groups. The code exception handling in the previous project was very chaotic. Now the code quality in the project has been improved, so I want to write an article about exception handling to discuss exception handling
1. Principles for catching exceptions
Catch an exception to handle it, don’t catch and then do nothing, code like the following makes no sense
public static void division() throws Excception { try { int i = 15/0; } catch(Exception e){ throw e; }}Copy the code
Exception information is not recorded or is not standardized. As a result, exceptions are swallowed, which makes fault locating difficult
public static void division() throws MyExcception { try { int i = 15/0; } catch(Exception e){ e.getMesage(); }}Copy the code
Loggger.debug (” XXXXXXXXXXX “,e);
2. Refine exceptions
Don’t catch Exception in general
3. Prevent null pointer exceptions
Null Pointers are the most common exception in a program
1, automatic unpacking may be null pointer
2. There may be empty Pointers when fetching elements from a collection
Obj.get ().get().get().get(); Null-pointer exceptions may occur
The result of the remote call may be null pointer exception
5. The data obtained in session may be null
6. Database query results may return NULL
4: LLDB etMessage(), e.tostring (), e.printStackTrace(), + E
Note the exception stack information, including exception type, exception description, and exception code location. LLDB etMessage() and E.tostring () are abnormal. E.printstacktrace () is printed on the console for easy viewing, so it cannot be used in formal code. +e is not the standard way to write it. If there is sensitive information in the code,+e will print sensitive information
5, Finally block
For file IO operations, database connection operations, or other connection operations, be sure to close its connection in Finally block, to avoid resource waste over time resulting in memory overflow
Do not have a return operation on a Finally block. A return operation causes a method to return, which may result in code not being executed outside the exception block. Be careful with a return operation on a Finally block
6. Custom exceptions
This normal, original exception may not be able to express our business meaning, so we will define our own exception, with error code and error code description, to better express the business error. Here I define the exception is checked, of course, you can also define RuntimeException. Checked exceptions that the caller must handle. The RuntimeException caller does not want to handle it explicitly.
package com.my.test.Exception; public class MyExcception extends Exception { private String code; private String message; public MyExcception(){ super(); } public MyExcception(Exception e){ super(e); } public MyExcception(String code,String message){ this.code = code; this.message = message; } public MyExcception(String code,String message,Exception e){ super(e); this.code = code; this.message = message; } } package com.my.test.Exception; public class Test { public static void main(String[] args) throws MyExcception { int n = division(); System.out.println(n); } public static int division() throws MyExcception { try { int i = 15/0; return i; } catch(Exception e){throw new MyExcception("10001"," parameter cannot be 0"); }}} it would be nice to log here and print the original stack informationCopy the code
7. View abnormal information
Normal business code we have a long chain of calls from the Controler to the Service to the DAO, and there are various business processes in between. Then I throw a custom exception and see that the error stack is very long. How can I quickly see where the code went wrong
package com.my.test.Exception; public class Test { public static void main(String[] args) throws MyExcception { int n = before(); System.out.println(n); } public static int before () throws MyExcception { System.out.println("begin........" ); return division(); } public static int division() throws MyExcception { try { int i = 15/0; return i; } catch(Exception e){throw new MyExcception("10001"," parameter cannot be 0",e); } } } begin........ Exception in thread "main" com.my.test.Exception.MyExcception: java.lang.ArithmeticException: / by zero at com.my.test.Exception.Test.division(Test.java:22) at com.my.test.Exception.Test.before(Test.java:13) at com.my.test.Exception.Test.main(Test.java:6) Caused by: java.lang.ArithmeticException: / by zero at com.my.test.Exception.Test.division(Test.java:19) ... We're going to see 2 more under Caused by, under Caused by the back is the location of the error code Here is no custom Exception, abnormal have thrown package com. My. Test. The Exception; public class Test { public static void main(String[] args){ int n = before(); System.out.println(n); } public static int before (){ System.out.println("begin........" ); return division(); } public static int division() { int i = 15/0; return i; } } begin........ Exception in thread "main" java.lang.ArithmeticException: / by zero at com.my.test.Exception.Test.division(Test.java:17) at com.my.test.Exception.Test.before(Test.java:12) at com.my.test.Exception.Test.main(Test.java:6) Process finished with exit code 1Copy the code