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 ...
随机推荐
- poj 3311 状压DP
经典TSP变形 学到:1.floyd O(n^3)处理随意两点的最短路 2.集合的位表示,我会在最后的总结出写出.注意写代码之前一定设计好位的状态.本题中,第0位到第n位分别代表第i个城市,1是已经 ...
- 怎样从Hadoop安全模式中进入正常模式
问题: 在Hadoop中,新建一个文件夹,报错了,提示mkdir: org.apache.hadoop.hdfs.server.namenode.SafeModeException: Cannot c ...
- const使用摘要
const在四种方案如以下: int b = 500; const int *a = &b; ①(底层const) int const *a = &b; ②(底层const) int ...
- 用MODELLER构建好模型后对loop区域进行自动的优化过程
一:对生成的模型的所有的loop区域进行优化 # Homology modeling by the automodel class from modeller import * from modell ...
- 重新想象 Windows 8 Store Apps (13) - 控件之 SemanticZoom
原文:重新想象 Windows 8 Store Apps (13) - 控件之 SemanticZoom [源码下载] 重新想象 Windows 8 Store Apps (13) - 控件之 Sem ...
- C# Windows Phone 8 WP8 , 文字超连结到网页 免打程式码,Xaml就搞定 ! !
原文:C# Windows Phone 8 WP8 , 文字超连结到网页 免打程式码,Xaml就搞定 ! ! 一般我们在开发Windows Phone 8 APP ,有时会需要超连结连到其他的网页,但 ...
- TMS320F28335项目开发记录2_CCS与JTAG仿真器连接问题汇总
CCS与仿真器连接问题 实际使用过程中.仿真器和CCS连接可能出现这样或那样的问题,或许你的连接非常成功,没碰到过什么问题.但我的问题的确不少,可能与电脑配置有关吧,也可能与人品有关吧. 以下的自己的 ...
- BeagleBone Black 板第三课:Debian7.5系统安装和远程控制BBB板
BBB板第三课:Debian7.5系统安装和远程控制BBB板 由于BBB板系统是Debian 7.4.据说使用Debian系统能够实现非常多BBB板的无缝连接.能够更好的学习和控制BBB板,所以就决定 ...
- vmware 10 注冊码
先安装VMware Workstation 10.0原版,然后用以下的随意一个VMware Workstation序列号注冊 1Y0LW-4WJ9N-LZ5G9-Z81QP-92PN7 JU052-F ...
- COM模块三---根的形成和注册代理server(Building and Registering a Proxy DLL)
Prerequisite:C++ 程序员,熟windows计划,熟Win32 Dll,了解windows注册表. 笔者:割者 上一篇文章中,我们定义了COM接口.通过编译生成了四个文件,本文使用这四个 ...