java 又一次抛出异常 相关处理结果演示样例代码

package org.rui.ExceptionTest;
/**
* 又一次抛出异常
*
在某些情况下,我们想又一次掷出刚才产生过的违例,特别是在用Exception 捕获全部可能的违例时。因为我
们已拥有当前违例的句柄,所以仅仅需简单地又一次掷出那个句柄就可以。以下是一个样例:
catch(Exception e) {
System.out.println("一个违例已经产生");
throw e;
}
又一次“掷”出一个违例导致违例进入更高一级环境的违例控制器中。 用于同一个try 块的不论什么更进一步的
catch 从句仍然会被忽略。 此外,与违例对象有关的全部东西都会得到保留。所以用于捕获特定违例类型的
更高一级的控制器能够从那个对象里提取出全部信息。
若仅仅是简单地又一次掷出当前违例,我们打印出来的、与printStackTrace()内的那个违例有关的信息会与违
例的起源地相应。而不是与又一次掷出它的地点相应。若想安装新的堆栈跟踪信息,可调用
fillInStackTrace(),它会返回一个特殊的违例对象。这个违例的创建步骤例如以下:将当前堆栈的信息填充到
原来的违例对象里。以下列出它的形式:
* @author lenovo
*
*/
public class Rethrowing {
public static void f() throws Exception
{
System.out.println("f() 方法运行");
throw new Exception("thrown form f()");
} public static void g() throws Exception
{
try {
f();
} catch (Exception e) {
System.out.println("inside g() ,e...");
e.printStackTrace(System.out);
throw e;//把补获的异常 又一次抛出异常
}
} public static void main(String[] args)
{
try {
g();
} catch (Exception e) {
System.out.println("main 处理异常 ");
e.printStackTrace(System.out);
} } }
/**
f() 方法运行
inside g() ,e...
java.lang.Exception: thrown form f()
at org.rui.ExceptionTest.Rethrowing.f(Rethrowing.java:7)
at org.rui.ExceptionTest.Rethrowing.g(Rethrowing.java:13)
at org.rui.ExceptionTest.Rethrowing.main(Rethrowing.java:25)
main 处理异常
java.lang.Exception: thrown form f()
at org.rui.ExceptionTest.Rethrowing.f(Rethrowing.java:7)
at org.rui.ExceptionTest.Rethrowing.g(Rethrowing.java:13)
at org.rui.ExceptionTest.Rethrowing.main(Rethrowing.java:25)
*/
package org.rui.ExceptionTest;

/**
* 可能在捕获异常之后抛出还有一种异常。 有关原来的异常发生点的信息会丢失。
* 剩下的是与新的抛出点有关的信息
* @author lenovo
*
*/ //自己定义异常
class OneException extends Exception
{
OneException(String s){ super(s);}
}
class TwoException extends Exception
{
TwoException(String s){ super(s);}
} ////////
public class RethrowNew {
public static void f() throws OneException
{
System.out.println("运行f方法 f()");
throw new OneException("thrown from f()");//
} public static void main(String[] args)
{ try {
try {
f();
} catch(OneException e)
{
System.out.println("内部异常处理, e.printStackTrace()");
e.printStackTrace();
throw new TwoException("抛出异常 inner try");
} } catch (TwoException e) {
System.out.println("outer try ---外部的异常处理");
e.printStackTrace(System.out);
}
}
}
/**
* 最后那个异常仅知道自已来自main 而对f()一无所知
*
output:
运行f方法 f()
内部异常处理, e.printStackTrace()
org.rui.ExceptionTest.OneException: thrown from f()
at org.rui.ExceptionTest.RethrowNew.f(RethrowNew.java:18)
at org.rui.ExceptionTest.RethrowNew.main(RethrowNew.java:26)
outer try ---外部的异常处理
org.rui.ExceptionTest.TwoException: 抛出异常 inner try
at org.rui.ExceptionTest.RethrowNew.main(RethrowNew.java:31) */ ///:~
package org.rui.ExceptionTest;

import java.util.Date;

/**
* 字符串格式化 演示样例
*
* 当中float类型与double类型的数据,对于String.format()方法来说,全表示为浮点数,可使用的格式化參数如:
String.format("%a, %e, %f, %g",floatType,floatType,floatType,floatType);
当中
%a 表示用十六进制表示
%e 表示用科学记数法表示
%f 表示用普通的10进制方式表示
%g 表示依据实际的类型的值的大小,或採用%e的方式,或採用%f的方式 对于日期类型的:
如:
String dataStr = String.format("%1$tm-%1$te-%1$tY",dateType);
当中1$表示假设參数中有多个dateType那么取哪个dateType中的值,
t表示日期或时间格式,
m表示月,e表示日,Y表示年.
*/
public class StringFormatTest { public static void main(String[] args) {
float floatType=1000.00f;
double doubleTyep=11111111111111111.00d;
Date dateType = new Date();
String floatStr = String.format("%a, %e, %f, %g",floatType,floatType,floatType,floatType);
String doubleStr = String.format("%a, %e, %f, %g",doubleTyep,doubleTyep,doubleTyep,doubleTyep);
String dataStr = String.format("%1$tm-%1$te-%1$tY",dateType);
System.out.println(floatStr);
System.out.println(doubleStr);
System.out.println(dataStr);
} } /** output:
0x1.f4p9, 1.000000e+03, 1000.000000, 1000.00
0x1.3bcbf936b38e4p53, 1.111111e+16, 11111111111111112.000000, 1.11111e+16
06-26-2014
*/

最新文章

  1. Core Java 总结(异常类问题)
  2. 关于API认证的问题
  3. InnoDB: Error number 24 means ‘Too many open files’.--转载
  4. Python学习路程day7
  5. 齐次坐标概念&&透视投影变换推导
  6. CentOS安装tomcat
  7. sql语句面试总结
  8. cookie保存分页参数
  9. hdu 6199 沈阳网络赛---gems gems gems(DP)
  10. openGL光源概念
  11. Zabbix实战-简易教程(2)--整体架构图
  12. python之路(七)-递归算法
  13. 阻塞式I/O实现简单TCP通信
  14. Vue系列之 => 评论功能(小知识点串联)
  15. python 离散序列 样本数伸缩(原创)
  16. java outterLoop跳出多重循环用法以及详解
  17. bzoj3932 / P3168 [CQOI2015]任务查询系统(主席树+差分)
  18. Drupal错误:drupal Maximum execution time of 30 seconds exceeded database in解决方法
  19. yii验证系统学习记录,基于yiicms(二)
  20. 【BZOJ】1613: [Usaco2007 Jan]Running贝茜的晨练计划(dp)

热门文章

  1. Trapping Rain Water leetcode java
  2. 一个执行Dos命令的窗口程序,与各位分享。
  3. 一步步教你如何在 Visual Studio 2013 上使用 Github
  4. python数据库访问
  5. mac利用Synergy操作多台电脑
  6. youtube相关
  7. android 实现qq聊天对话界面效果
  8. PD的CDM模型中的三种实体关系
  9. head 命令(转)
  10. 文本框只支持数字、小数点、退格符、负号、Del键