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


posted on 2017-08-11 14:24  yutingliuyl  阅读(178)  评论(0编辑  收藏  举报