博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java通过抛异常来返回提示信息
阅读量:6967 次
发布时间:2019-06-27

本文共 3174 字,大约阅读时间需要 10 分钟。

结论:

如果把通过抛异常的方式得到提示信息,可以使用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     * Throwable object information about the current state of     * the stack frames for the current thread.     *     * @return  a reference to this Throwable 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相同 

转载地址:http://tnisl.baihongyu.com/

你可能感兴趣的文章
收快递成“新开门七件事” 京东小哥最暖心
查看>>
AMD又有大动作!2018CES期间牵手京东强势吸睛
查看>>
2017百度AI开发者大会 一场5000名开发者的分享盛宴
查看>>
野心外漏?Windows Defender或将独霸杀毒软件市场?
查看>>
重庆“90后”双胞胎“动妹” 守护春运回家路
查看>>
电影《蓝色生死恋》将上映 保留原版经典片段
查看>>
“中华龙乡”重庆铜梁举办首届中华龙灯艺术节
查看>>
探访广铁深圳“父女搭档”乘警出勤风采
查看>>
6月Python热文Top10,精选自1000篇文章
查看>>
Vue 折腾记 - (12) Nuxt.js写一个校验访问浏览器设备类型及环境的中间件
查看>>
使用 React 全家桶搭建一个后台管理系统
查看>>
腾讯云容器团队内部Istio专题分享
查看>>
当我说要做大数据工程师时他们都笑我,直到三个月后……
查看>>
【数据科学系统学习】Python # 数据分析基本操作[二] pandas
查看>>
第一批95后已经是阿里科学家了
查看>>
第七章: ansible故障排查
查看>>
everything is object
查看>>
Android中的设计模式之单例模式
查看>>
webpack核心概念
查看>>
Vue 兼容 ie9 的全面解决方案
查看>>