结论:
如果把通过抛异常的方式得到提示信息,可以使用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通过抛异常来返回提示信息的更多相关文章

  1. 编写高质量代码改善C#程序的157个建议[用抛异常替代返回错误、不要在不恰当的场合下引发异常、重新引发异常时使用inner Exception]

    前言 自从.NET出现后,关于CLR异常机制的讨论就几乎从未停止过.迄今为止,CLR异常机制让人关注最多的一点就是“效率”问题.其实,这里存在认识上的误区,因为正常控制流程下的代码运行并不会出现问题, ...

  2. JAVA主动抛异常的几种方式及捕捉结果输出对比

    测试代码: /** * 测试异常抛出及捕捉 */ @Test public void test() { try { this.testA(); } catch (Exception ex) { Sys ...

  3. 点评阿里JAVA手册之异常日志(异常处理 日志规约 )

    下载原版阿里JAVA开发手册  [阿里巴巴Java开发手册v1.2.0] 本文主要是对照阿里开发手册,注释自己在工作中运用情况. 本文内容:异常处理 日志规约 本文难度系数为一星(★) 本文为第三篇 ...

  4. Java throw:异常的抛出怎么回事

    到目前为止,你只是获取了被Java运行时系统抛出的异常.然而,程序可以用throw语句抛出明确的异常.Throw语句的通常形式如下:    throw ThrowableInstance;这里,Thr ...

  5. java 检查抛出的异常是否是要捕获的检查性异常或运行时异常或错误

    /** * Return whether the given throwable is a checked exception: * that is, neither a RuntimeExcepti ...

  6. 扩展方法where方法查询不到数据,不会抛异常,也不是返回的null

    如题,“扩展方法where方法查询不到数据,不会抛异常,也不是返回的null”,示例代码如下: Product类: public class Product { private string name ...

  7. Android ADT插件更新后程序运行时抛出java.lang.VerifyError异常解决办法

    当我把Eclipse中的 Android ADT插件从21.1.0更新到22.0.1之后,安装后运行程序抛出java.lang.VerifyError异常. 经过调查,终于找到了一个有效的解决办法: ...

  8. 千万别在Java类的static块里写会抛异常的代码!

    public class Demo{ static{ // 模拟会抛异常的代码 throw new RuntimeException(); } } 如果你在Java类的static块里写这样会抛异常的 ...

  9. AOP拦截日志类,抛异常:java.lang.IllegalStateException: It is illegal to call this method if the current request is not in asynchronous mode

    AOP的日志拦截类中,抛出异常: java.lang.IllegalStateException: It is illegal to call this method if the current r ...

随机推荐

  1. JavaScript的作用域和变量对象

    变量对象 先来说说什么是变量对象.变量对象中又存储了什么东西吧. JavaScript中的运行环境包含全局运行环境和函数运行环境这两种,每进入到一个运行环境都会创建一个变量对象,这个对象中记录了在当前 ...

  2. Visual Studio跨平台开发实战(2) - Xamarin.iOS基本控制项介绍

    原文 Visual Studio跨平台开发实战(2) - Xamarin.iOS基本控制项介绍 前言 在上一篇文章中, 我们介绍了Xamarin 以及简单的HelloWorld范例, 这次我们针对iO ...

  3. PHP 字符串正则替换函数preg_replace使用说明

    1. preg_replace() $msg = preg_replace("/<style>.+<\/style>/is", "", ...

  4. Big Event in HDU(杭电1171)(多重背包)和(母函数)两种解法

    Big Event in HDU Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

  5. Web中的性能优化

    优化Web中的性能 简介 web的优化就是一场阻止http请求最终访问到数据库的战争.优化的方式就是加缓存,在各个节点加缓存. web请求的流程及节点 熟悉流程及节点,才能定位性能的问题.而且优化的顺 ...

  6. gopkg:一种方便的go pakcage管理方式

    在使用go的过程中,我们有时候会引入一些第三方库来使用,而通常的方式就是使用go get,可是这样的方式有一个非常严重的问题,假设第三方库更新了相关接口,非常有可能你就无法使用了,所以我们一套非常好地 ...

  7. java HashMap中出现反复的key, 求解释

    上代码: Person p1 = new Person("xiaoer",1); Person p2 = new Person("san",4); Map< ...

  8. Naive Bayes Classification

    Maching Learning QQ群:2 请说明来自csdn 微信:soledede

  9. [置顶] ffmpg简介以及用它实现音频视频合并(java)

    1.简介     FFmpeg是一个自由软件,可以运行音频和视频多种格式的录影.转档.流功能. 2.下载     源代码 git://git.libav.org/libav.git     Windo ...

  10. 发布Ubuntu/Linux系统cache,增加可用内存空间

    桌面Ubuntu总内存4G,但free只有内存有100M 重视top命令检查看到真正的能力free内存.以下是真正的内存使用情况的看法有一个命令. watch -n 1 cat /proc/memin ...