zoukankan      html  css  js  c++  java
  • 异常-finally关键字的特点及作用

     1 package cn.itcast_07;
     2 
     3 import java.text.ParseException;
     4 import java.text.SimpleDateFormat;
     5 import java.util.Date;
     6 
     7 /*
     8  * finally:被finally控制的语句体一定会执行
     9  * 注意:如果在执行到finally之前jvm退出了,就不能执行了。
    10  * 
    11  * A:格式
    12  *         try...catch...finally...
    13  * B:用于释放资源,在IO流操作和数据库操作中会见到
    14  */
    15 public class FinallyDemo {
    16     public static void main(String[] args) {
    17         String s = "2014-11-20";
    18         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    19 
    20         Date d = null;
    21         try {
    22             // System.out.println(10 / 0);可能有这句话,抛异常,所以要先设置Date d = null;进行初始化
    23             d = sdf.parse(s);
    24         } catch (ParseException e) {
    25             e.printStackTrace();
    26             System.exit(0);//jvm退出
    27         } finally {
    28             System.out.println("这里的代码是可以执行的");
    29         }
    30 
    31         System.out.println(d);
    32     }
    33 }
  • 相关阅读:
    OSI模型白话
    并发
    初始化与清理
    多线程
    recyclerview Adapter
    recyclerview刷新
    surfaceview
    viewgroup绘制流程
    view配置
    项目遇到的问题
  • 原文地址:https://www.cnblogs.com/ZHOUVIP/p/7221262.html
Copyright © 2011-2022 走看看