Java异常处理中的两种特殊情况

原创|其它|编辑:郝浩|2010-03-11 11:35:46.000|阅读 626 次

概述:本文介绍了Java异常处理中的两种特殊情况。

# 界面/图表报表/文档/IDE等千款热门软控件火热销售中 >>

  1、不能在finally块中执行return,continue等语句,否则会把异常“吃掉”;

  2、在try,catch中如果有return语句,则在执行return之前先执行finally块

  请大家仔细看下面的例子:

  以下是引用片段:


     public class TryTest {
  public static void main(String[] args) {
  try {
  System.out.println(TryTest.test());// 返回结果为true其没有任何异常
  } catch (Exception e) {
  System.out.println("Exception from main");
  e.printStackTrace();
  }
  doThings(0);
  }
  public static boolean test() throws Exception {
  try {
  throw new Exception("Something error");// 第1步.抛出异常
  } catch (Exception e) {// 第2步.捕获的异常匹配(声明类或其父类),进入控制块
  System.out.println("Exception from e");// 第3步.打印
  return false;// 第5步. return前控制转移到finally块,执行完后再返回(这一步被吃掉了,不执行)
  } finally {
  return true; // 第4步. 控制转移,直接返回,吃掉了异常
  }
  }
  public static void doThings(int i)
  {
  try
  {
  if(i==0)
  {
  //在执行return之前会先执行finally
  return;
  }
  int t=100/i;
  System.out.println(t);
  }catch(Exception ex)
  {
  ex.printStackTrace();
  }
  finally
  {
  System.out.println("finally");
  }
  }
  }


标签:

本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@evget.com

文章转载自:网络转载

为你推荐

  • 推荐视频
  • 推荐活动
  • 推荐产品
  • 推荐文章
  • 慧都慧问
扫码咨询


添加微信 立即咨询

电话咨询

客服热线
023-68661681

TOP