java 抛出异常 返回值_java通过抛异常来返回提示信息

结论:

如果把通过抛异常的方式得到提示信息,可以使用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

publicThrowable(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:

packageexception;/*2015-8-22*/

public classExceptionChain {public static voidmain(String[] args) {

Business business= newBusiness();try{

business.doBusiness();

}catch(ProcessorException e) {

System.out.println(e.getMessage());

System.out.println("e:\n" +e);

}

}

}classBusiness {public void doBusiness() throwsProcessorException {try{

process1();

}catch(Exception e) {throw newProcessorException(e);//throw new ProcessorException(e.getMessage(), e);//throw new ProcessorException(e.getMessage());

}

}private void process1() throwsProcessorException {try{

process2();

}catch(Exception e) {//throw new ProcessorException(e.getMessage(), e);//throw new ProcessorException(e.getMessage());

throw newProcessorException(e);

}

}private voidprocess2() {throw new IllegalArgumentException("Failt to process");

}

}class ProcessorException extendsException {private static final long serialVersionUID = -4270191862690602942L;publicProcessorException(Throwable cause) {super(cause);

}publicProcessorException(String message) {super(message);

}publicProcessorException(String message, Throwable cause) {super(message, cause);

}

}

2、通过java.lang.Throwable中的Constructs

publicThrowable(String message) {

fillInStackTrace();

detailMessage=message;

}

/*** Fills in the execution stack trace. This method records within this

* Throwable object information about the current state of

* the stack frames for the current thread.

*

*@returna reference to this Throwable instance.

*@seejava.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:

publicThrowable(String message, Throwable cause) {

fillInStackTrace();

detailMessage=message;this.cause =cause;

}

在输出时获取的detailMessage是:

Failt to process

Exception信息的输出:

exception.ProcessorException: Failt to process

code:

操作方式与2相同

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值