Java-源代码-简单例外捕获演练2

finally子句用来说明必须执行的语句,无论TRY程序块中是否抛出例外,FINALLY程序块中的语句都将会被执行..

 

 

public class ExampleFinallyException
{
 public static void main(String[] args)
 {
  System.out.println("Hello World!");
  try
  {
   int x=0;
   int y=28;
   int z=y/x;//除数为零..
   System.out.println("The value of y/x is "+z);
  }
  catch (ArithmeticException e)
  {
   System.out.println("this program has caught an Arithmetic Exception!");
  }
  finally
  {
   System.out.println("Excuting the finally sub sentence!"); 
   try
   {
    String name=null;
    if(name.equals("wangneng"))
     System.out.println("My name is wangneng!");
   }
   catch (Exception e)
   {
    System.out.println("this program has caught an Arithmetic Exception once more! ");
   }
   finally
   {
    System.out.println("Excuting the finally sub sentence once more!");
   }
  }
 }
}
 

 

finally子句通常还可以与break.continue and return语句一起使用.当try程序地中出现了break.continue and return等语句时,程序必须先执行finally子句,才能最终离开程序块..

class BreakAndFinally
{
 public static void main(String[] args)
 {
  System.out.println("Hello World!");
  while(true)
  {
   try
   {
    System.out.println("this program wanna go out of this while sub sentence by break !");
    break;
   }
   finally
   {
    System.out.println("this program must execute me before leave the while sub sentence");
   }
  }
 }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值