异常使用陷阱

来源:互联网 发布:snmp使用的端口号是 编辑:程序博客网 时间:2024/04/29 03:23

代码:

public class ExceptionTest5
{
 public void method()
 {
  try
  {
   System.out.println("进入到try块");
   
   System.exit(0);//这样调用的时候那么程序退出虚拟机了,所以finally里面的语句也不会执行

   return;//这样调用finally里面的语句还是会执行的,最后才会return;
  }
  catch(Exception ex)
  {
   System.out.println("异常发生了");
  }
  finally
  {
   System.out.println("进入到finally块");
  }
  
  System.out.println("异常处理后续的代码");
 }
 
 public static void main(String[] args)
 {
  ExceptionTest5 test = new ExceptionTest5();
  
  test.method();
 }

}

原创粉丝点击