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
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通过抛异常来返回提示信息的更多相关文章
- 编写高质量代码改善C#程序的157个建议[用抛异常替代返回错误、不要在不恰当的场合下引发异常、重新引发异常时使用inner Exception]
前言 自从.NET出现后,关于CLR异常机制的讨论就几乎从未停止过.迄今为止,CLR异常机制让人关注最多的一点就是“效率”问题.其实,这里存在认识上的误区,因为正常控制流程下的代码运行并不会出现问题, ...
- JAVA主动抛异常的几种方式及捕捉结果输出对比
测试代码: /** * 测试异常抛出及捕捉 */ @Test public void test() { try { this.testA(); } catch (Exception ex) { Sys ...
- 点评阿里JAVA手册之异常日志(异常处理 日志规约 )
下载原版阿里JAVA开发手册 [阿里巴巴Java开发手册v1.2.0] 本文主要是对照阿里开发手册,注释自己在工作中运用情况. 本文内容:异常处理 日志规约 本文难度系数为一星(★) 本文为第三篇 ...
- Java throw:异常的抛出怎么回事
到目前为止,你只是获取了被Java运行时系统抛出的异常.然而,程序可以用throw语句抛出明确的异常.Throw语句的通常形式如下: throw ThrowableInstance;这里,Thr ...
- java 检查抛出的异常是否是要捕获的检查性异常或运行时异常或错误
/** * Return whether the given throwable is a checked exception: * that is, neither a RuntimeExcepti ...
- 扩展方法where方法查询不到数据,不会抛异常,也不是返回的null
如题,“扩展方法where方法查询不到数据,不会抛异常,也不是返回的null”,示例代码如下: Product类: public class Product { private string name ...
- Android ADT插件更新后程序运行时抛出java.lang.VerifyError异常解决办法
当我把Eclipse中的 Android ADT插件从21.1.0更新到22.0.1之后,安装后运行程序抛出java.lang.VerifyError异常. 经过调查,终于找到了一个有效的解决办法: ...
- 千万别在Java类的static块里写会抛异常的代码!
public class Demo{ static{ // 模拟会抛异常的代码 throw new RuntimeException(); } } 如果你在Java类的static块里写这样会抛异常的 ...
- 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 ...
随机推荐
- srm 533
250 Description 给你一串数字序列,每次删掉第ii个数.获得权值w[i−1]×w[i+1]w[i-1]\times w[i+1],求最后剩下第一个和最后一个数获得的最大权值和 Solut ...
- Android 之流媒体播放器,广播侧下方这么简单。
没有其他的.希望从事流媒体开发案例.还承诺提供朋友博客.上个星期.制定出最后一点机会. 在这里,与大家分享. 首先要明白的概念:什么是流媒体?转载请注明出处http://blog.csdn.net/g ...
- javascript - 浏览TOM大叔博客的学习笔记
part1 ---------------------------------------------------------------------------------------------- ...
- poj 2586 Y2K Accounting Bug (贪心)
Y2K Accounting Bug Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8678 Accepted: 428 ...
- oracle 锁表、解锁的语句
--1.以下的语句用来查询哪些对象被锁: select object_name,machine,s.sid,s.serial# from v$locked_object l,dba_object ...
- 部署IIS错误
- HD2 Tmobile 重新分区代码(使用clk 1.6.5 de)
fastboot oem part-resize misc: fastboot oem part-resize recovery: fastboot oem part-resize boot: fas ...
- 【原创】纯OO:从设计到编码写一个FlappyBird (六)
第五部分请看这里 终于到了最后一个部分了! 这里使用SimpleJudge类来实现Judge接口. 首先是SimpleJudge需要的实例变量: 0.final LinkedList<Pilla ...
- ios-上拉电阻负载许多其他接口
想尝试拉加载意识到有多少开始了他的研究之旅,我看了两天做出最终的界面. 之所以这么慢是由于,我不知道要将上拉出现的view放在哪.就能在scrollView拉究竟部的时候被拉出来.还有就是怎么拉出来之 ...
- openGL研究钞四 : 关于颜色, 尺寸, 虚线, 多边形逆转, 空洞, 使用位图
转载请保留源,,,,hushuai1992http://blog.csdn.net/u013642494/article/category/2675731 额. 这个标题我都不知道该怎么起了. 假设没 ...