伪代码:
Res methodB() throws Exception { Res res = new Res(); ...... if(res.getResult == false) throws MyException("MyError:"+res.getMsg); return res; }
void methodA() { try{ Res res = mothodB(); log.info(res.toString()); } catch(MyException e) { log.error(e.getMessage()); // 可以精确定位问题代码行数,所以仅打印msg // log.error(e.getMessage(), m); // 可以打印堆栈,但无意义 } catch(Exception e) { log.error(e.getMessage(), m); // 其它运行期未知异常,打印完整含堆栈信息,也可以再向上抛出 } }
public class MyException extends Exception { private static final long serialVersionUID = 1364102728393959890L; public MyException() { super(); } public MyException(String message, Throwable cause) { super(message, cause); } public MyException(String message) { super(message); } public MyException(Throwable cause) { super(cause); } }