搜课云网 > 北大青鸟 > 资讯总汇 > Java程序异常处理的特殊情况

Java程序异常处理的特殊情况

机构:北大青鸟 时间:2016-01-25 10:29:57 点击:748

  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");

  }

  }

  }

师资介绍