结论:
如果把通过抛异常的方式得到提示信息,可以使用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. MVC的DependencyResolver组件

    MVC的DependencyResolver组件 一.前言 DependencyResolver是MVC中一个重要的组件,从名字可以看出,它负责依赖对象的解析,可以说它是MVC框架内部使用的一个IOC ...

  2. hdu5115(区间dp)

    n头狼排成一列,每头狼有两个属性,基础攻击力和附加攻击力, 第i只狼的基础攻击力是ai,附加攻击力是b(i-1) + b(i+1) 消灭一只狼,受到的伤害为基础攻击力+附加攻击力. 问消灭所有的狼受到 ...

  3. Solaris 10下使用Python3

    通常在Solaris 10上仅仅能使用Python2.x. 假设使用Python3的话,一种就是http://www.sunfreeware.com获取可用的二进制版本号.只是眼下这个站点已经不提供免 ...

  4. 利用Sambaserver在Ubuntu系统和Win7系统间共享目录

    1 介绍 如今是网络化的时代,我们每一个人要更好的发展.离不开网络化.信息化的支持.利用网络的支持.在不同的操作系统间共享文件等信息,是计算机专业学生必备的一项技能. 本文所讲的就是怎样建立.设置.链 ...

  5. 每天一点儿JAVA-向量的操作

    package java_prac2; import java.util.*; /** * <p>Title: 矢量操作</p> * <p>Description: ...

  6. Js常用技巧

    摘录:http://crasywind.blog.163.com/blog/static/7820316920091011643149/ js 常用技巧 1. on contextmenu=" ...

  7. 重新想象 Windows 8 Store Apps (11) - 控件之 ListView 和 GridView

    原文:重新想象 Windows 8 Store Apps (11) - 控件之 ListView 和 GridView [源码下载] 重新想象 Windows 8 Store Apps (11) - ...

  8. ContentProvider的使用

    这方面的资料应该网上已经很多了,我在这里只是做简单的总结就行了. 如题:ContentProvider是android的内容提供器,可以为应用程序提供各种的数据,例如数据表,txt文件,xml文件等等 ...

  9. Oracle 11G CRUD操作监控单个表

    前言:    线上oracle数据库有张表的数据有些乱,依据应用db的log和应用的log也没有检查出来谁改动了.所以决定把这张单表做个具体的insert.update.delete监控.一:使用数据 ...

  10. sublime配置攻略

    大家好,今天给大家分享的编辑器:sublime text2     我用过非常多编辑器, EditPlus.EmEditor.Notepad++.Notepad2.UltraEdit.Editra.V ...