java通过抛异常来返回提示信息
2015-08-22 21:08
585 查看
结论:
如果把通过抛异常的方式得到提示信息,可以使用java.lang.Throwable中的构造函数:
或
原因及代码示例:
1、通过java.lang.Throwable中的Constructs
在输出时获取的detailMessage是:
Exception信息的输出:
code:
2、通过java.lang.Throwable中的Constructs
在输出时获取的detailMessage是:
Exception信息的输出:
code:
把上面示例代码中throw new ProcessorException(e.getMessage());注释去掉,把 // throw new ProcessorException(e);注释
3、通过java.lang.Throwable中的Constructs:
在输出时获取的detailMessage是:
Exception信息的输出:
code:
操作方式与2相同
如果把通过抛异常的方式得到提示信息,可以使用java.lang.Throwable中的构造函数:
public Throwable(String message) { fillInStackTrace(); detailMessage = message; }
或
public Throwable(String message, Throwable cause) { fillInStackTrace(); detailMessage = message; this.cause = cause; }
原因及代码示例:
1、通过java.lang.Throwable中的Constructs
public Throwable(Throwable cause) { fillInStackTrace(); detailMessage = (cause==null ? null : cause.toString()); this.cause = cause; }
在输出时获取的detailMessage是:
exception.ProcessorException: java.lang.IllegalArgumentException: Failt to process
Exception信息的输出:
exception.ProcessorException: exception.ProcessorException: java.lang.IllegalArgumentException: Failt to process
code:
package exception; /*2015-8-22*/ public class ExceptionChain { public static void main(String[] args) { Business business = new Business(); try { business.doBusiness(); } catch (ProcessorException e) { System.out.println(e.getMessage()); System.out.println("e:\n" + e); } } } class Business { public void doBusiness() throws ProcessorException { try { process1(); } catch (Exception e) { throw new ProcessorException(e); // throw new ProcessorException(e.getMessage(), e); // throw new ProcessorException(e.getMessage()); } } private void process1() throws ProcessorException { try { process2(); } catch (Exception e) { // throw new ProcessorException(e.getMessage(), e); // throw new ProcessorException(e.getMessage()); throw new ProcessorException(e); } } private void process2() { throw new IllegalArgumentException("Failt to process"); } } class ProcessorException extends Exception { private static final long serialVersionUID = -4270191862690602942L; public ProcessorException(Throwable cause) { super(cause); } public ProcessorException(String message) { super(message); } public ProcessorException(String message, Throwable cause) { super(message, cause); } }
2、通过java.lang.Throwable中的Constructs
public Throwable(String message) { fillInStackTrace(); detailMessage = message; }
/** * Fills in the execution stack trace. This method records within this * <code>Throwable</code> object information about the current state of * the stack frames for the current thread. * * @return a reference to this <code>Throwable</code> instance. * @see java.lang.Throwable#printStackTrace() */ public synchronized native Throwable fillInStackTrace();
在输出时获取的detailMessage是:
Failt to process
Exception信息的输出:
exception.ProcessorException: Failt to process
code:
把上面示例代码中throw new ProcessorException(e.getMessage());注释去掉,把 // throw new ProcessorException(e);注释
3、通过java.lang.Throwable中的Constructs:
public Throwable(String message, Throwable cause) { fillInStackTrace(); detailMessage = message; this.cause = cause; }
在输出时获取的detailMessage是:
Failt to process
Exception信息的输出:
exception.ProcessorException: Failt to process
code:
操作方式与2相同
相关文章推荐
- 解析Java反射java.lang.IllegalArgumentException: wrong number of arguments
- Java 实现PAT乙级(Basic Level)1001-1010 解题报告(一)
- Java中的正则表达式
- Java中的正则表达式
- 使用Eclipse查看java源码
- java中string与json互相转化
- java file 删除目录下无用的文件
- spring 注解
- Exception in thread " " java.lang.UnsupportedClassVersionError: 错误
- Eclipse创建Struts项目
- Java------类加载机制
- Java- 对象清楚与垃圾回收
- Java Map接口Properties
- Java生成CSV文件
- Java字符串操作、基本运算方法等优化策略
- Java 注解的简单学习与应用 Annotation
- Java Map接口TreeMap
- eclipse最有用快捷键整理
- Spring-管理bean
- Java学习笔记,基础总结(一)