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
*/