码迷,mamicode.com
首页 > 其他好文 > 详细

异常处理、常用类

时间:2016-06-26 23:56:14      阅读:267      评论:0      收藏:0      [点我收藏+]

标签:

 Exception

 

Thowable分为Error、Exception

RuntimeException 非检查异常,可以不用强制捕获

捕获异常

  try{

     可能出现异常的代码

    }

  catch(Exception e1){

      捕获块

    }

  catch(Exception e2){

      捕获块

    }

  catch(Exception e3){

      捕获块

    }

  .........

  catch(Exception en){

      捕获块

    }

  finally{

    不管如何都会执行,一般放入资源关闭。

    }

 

  throws关键字 抛出异常,调用此方法的类就必须处理异常

  throw关键字,手动抛出一个异常对象

  方法一

    Exception e=new Exception("范围不合理“);

    throw e;

  方法二

    throw new Exception("范围不合理“);

 

常用类

  System类

  System.out输出流  System.in输入流

  System.exit(0);//正常退出

  Runtime类

  Runtime.exec("运行程序");//打开某个应用程序

  String类

  Boolean a=str1.equalsIgnoreCase(str2);//忽略大小写比较两个String类型数据是否相等

  int b=str.indexof(str1);//得到str里面str1所在的索引值,如果包含多个,返回的是索引值小的。如果不包含。则返回-1.

  str.length();//字符串的长度

  charAt(index);

  str.toUpercase();//转换成大写

  str.toLowercase();//转换成小写

  str.concat("a");//连接

  String[]strs=str.split(":");//根据:把str分为数组

  str.startswith("a");//是否是以”a"开始

  str.endswith("b");//是否是以”a"结尾

  str.replace("a","b");//把str里面的a全部替换为

  StringBuffer方法时线程安全,StringBuilder不是。StringBuilder比StringBuffer性能略高。

 

  Math类

  Math.PI;

  Math.random();//0.0到1.0的随机数

  Math.floor(3.9);//得到double的3.0

  Math.round(3.9);//得到四舍五入的Long型数据

  Math.abs(x);//x的绝对值

  double a=Math.pow(2,3);//2的3次方

  Date类

  Long beginTime=System.currentTimeMillis();//获取系统时间

  Long endTime=System.currentTimeMillis();

  Date date=new Date();

  SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd  hh:mm:ss");

  String dates=sf.format(date);

  System.out.println(dates);//输出系统当前时间

 

异常处理、常用类

标签:

原文地址:http://www.cnblogs.com/czliang/p/5618807.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!