Java异常、常用类

java异常

01-异常的概念

程序运行过程中因为错误发生的中断

02-捕获和处理异常

java中我们用try…catch…来捕获异常

try…catch…finally

不论前面是否是return或者结束语句,finally里面的语句总会执行到!

public class test
{
    public static void testFinally()
    {
        String str="123a";
        try
        {
            //将可能异常的代码放在try里
            int a=Intefer.parseInt(str);
            System.out.println(a);
        }
        catch(Exception e)
        {
            //Exception异常类
            //异常类中的方法
            e.printStackTrace();
            System.out.println("Exception");
            return;		//如果异常就返回
        }
        catch(Exception e)
        {
            //可以多个catch一起用
        }
        finally
        {
            System.out.println("final end")
        }
        System.out.println("end");
        
        //当使用了finally的时候"final end"和"end"都能打印出来了!
    }
}
03-throws和throw
  • throws表示当前方法不处理异常,而是交给方法的调用去处理
  • throw表示直接抛出一个异常
public class test
{
    public static void testThrows() throws NumberFornatException
        //throws 表示抛出异常
        //NumberFornatException表示异常的类型
    {
        String str="123a";
        int a=Intefer.parseInt(str);
        System.out.println(a);
    }
    
    public static void main(String[] args)
    {
        try
        {
            testThrows();
            System.out.println("here")	//不会打印here    
        }
        catch(Exception e)
        {
            System.out.println("我们在这里处理了异常")
                e.printStackTrace();
        }
        System.out.println("I an here");
        
        //结果:"我们在这里处理了异常" "I an here"

    }
    
}
public class test
{
    public static void testThrow(int a) throws Exception
    {
        if(a==1)
        {
            throw new Exception("有异常");
        }
        System.out.println(a);
    }
    
    public static void main(String[] args)
    {
        try
        {
            testThrow(1);
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
        
        //结果:"有异常"
        
        try
        {
            testThrow(0);
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
		
        //结果:0
    }
}
04-Exception和RuntimeException
  • Exception

    检查型异常,例如Exception在程序中必须使用try…catch进行处理
    编译时异常

  • RuntimeException

    非检查型异常,例如NumberFormatException,可以不使用try…catch进行处理,但是如果产生异常,则将异常由JVM(虚拟机)进行处理
    运行时异常

建议:RuntimeException最好也用try…catch捕获

public static void test()throws RuntimeException
{
    throw new RuntimeException("运行时异常");
    public static main(String[] args)
    {
        test();		//可以不用try捕获
    }
}
05-自定义异常

业务上的异常体系

异常类继承exception类就行了,相当于子类

改写,调用父类的方法

Java常用类

01-日期处理类
  • Data

    import java.util.Date;
    
    import javax.xml.crypto.Data;
    
    public class test
    {
     
       public static void main(String[] args) 
        {
            Date date=new Date();
            System.out.println(date);
           //Sun Jun 21 16:18:27 CST 2020
        }
    }
    
  • Calender

    import java.util.Calendar;
    import jdk.internal.org.jline.reader.Candidate;
    
    public class test
    {
     
       public static void main(String[] args) 
        {
           Calendar calendar=Calendar.getInstance();    //实例化Calendar,不可用new,要用getInstance接口
           System.out.println(calendar.get(Caldendar.YEAR));
        }
    }
    
  • SimpleDateFormat

    将日期解析为文本,将文本解析为日期等

import java.text.SimpleDateFormat;
import java.util.Date;

public class Animal
{
 
   public static void main(String[] args) 
    {
      Date date=new Date();
      SimpleDateFormat sdf=new SimpleDateFormat();
      System.out.println(sdf.format(date));
       //结果:2020/6/21 下午4:32
    }
}
02-String和StringBuffer类
  • String

    对String类型的对象操作,等同于重新生成一个对象,然后将引用指向它。

  • StringBuffer

    对StringBuffer类型的对象操作,操作始终是用一个对象

public class test
{
    String str="123";
    str+="abc";
    System.out.println(str);
    
    StringBuffer strb=new StringBuffer("123");
    strb.append("abc");
    System.out.println(strb);
    
}

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-tP6MU8VE-1592742901226)(C:\Users\Administrator\Desktop\资料,源码\java\笔记\图片\str strb.png)]

03-Math类
  • max 求最大值
  • min 求最小值
  • round 四舍五入
  • pow 求次幂
public class test
{
 
   public static void main(String[] args) 
    {
     	System.out.println(Math.max(1,2));
    }
}
04-Arrays类
  • toString() 返回指定数组内容的字符串表示形式
  • sort() 对于指定的类型数组数字升序进行排序
  • binarySearch() 是用二分搜索法来搜索指定类型数组,以获得指定的值
  • fill() 将指定类型值分配给指定类型数组的每个元素
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值