异常体系

异常的处理
throw抛出
public class ThrowDemo {
    public static void main(String[] args) {
        //创建一个数组 
        int[] arr = {2,4,52,2};
        //根据索引找对应的元素 
        int index = 4;
        int element = getElement(arr, index);
        System.out.println(element);
        System.out.println("over");
    }
    /*
     * 根据 索引找到数组中对应的元素
     */
    public static int getElement(int[] arr,int index){ 
        if(arr == null){
            /*
             判断条件如果满足,当执行完throw抛出异常对象后,方法已经无法继续运算。
             这时就会结束当前方法的执行,并将异常告知给调用者。这时就需要通过异常来解决。 
              */
            throw new NullPointerException("要访问的arr数组不存在");
        }
           //判断  索引是否越界
        if(index<0 || index>arr.length-1){
             /*
             判断条件如果满足,当执行完throw抛出异常对象后,方法已经无法继续运算。
             这时就会结束当前方法的执行,并将异常告知给调用者。这时就需要通过异常来解决。 
              */
             throw new ArrayIndexOutOfBoundsException("哥们,角标越界了~~~");
        }
        int element = arr[index];
        return element;
    }
}
throws声明
import java.io.File;
import java.io.FileNotFoundException;
public class TestException {
    public static void main(String[] args) throws FileNotFoundException {
        readFile("不敲代码学会Java秘籍.txt");
    }
    // 如果定义功能时有问题发生需要报告给调用者。可以通过在方法上使用throws关键字进行声明
    public static void readFile(String filePath) throws FileNotFoundException{
        File file = new File(filePath);
        if(!file.exists()){
            throw new FileNotFoundException(filePath+"文件不存在");
        }
    }    
}
try-catch捕获
 try{
 }catch(...){
 }finally{
     无论try中是否发生异常,也无论catch是否捕获异常,也不管try和catch中是否有return语句,都一定会执行
 }
面试题
public static void main(String[] args) {
    int test = test(3,5);
    System.out.println(test);//8
}
public static int test(int x, int y){
    int result = x;
    try{
        if(x<0 || y<0){
            return 0;
        }
        result = x + y;
        return result;
    }finally{
        result = x - y;
    }
}
public class Test04 {
    static int i = 0;
    public static void main(String[] args) {
        System.out.println(test());//2
    }
    public static int test(){
        try{
            return ++i;
        }finally{
            return ++i;
        }
    }
}
异常的注意事项
- 运行时异常被抛出可以不处理。即不捕获也不声明抛出
 - 如果finally有return语句,永远返回finally中的结果
 - 如果父类抛出了多个异常,子类重写父类方法时,抛出和父类相同的异常或者是父类异常的子类或者不抛出异常
 - 父类方法没有抛出异常,子类重写父类该方法时也不可抛出异常。此时子类产生该异常,只能捕获处理,不能声明抛出
 
包装类
| 序号 | 基本数据类型 | 包装类(java.lang包) | 
|---|---|---|
| 1 | byte | Byte | 
| 2 | short | Short | 
| 3 | int | Integer | 
| 4 | long | Long | 
| 5 | float | Float | 
| 6 | double | Double | 
| 7 | char | Character | 
| 8 | boolean | Boolean | 
| 9 | void | Void | 
装箱与拆箱
JDK1.5后,可以子哦对那个装箱与拆箱
只能与自己对应的类型间才能实现自动装箱与拆箱
Integer i = 4;//自动装箱。相当于Integer i = Integer.valueOf(4); i = i + 5;//等号右边:将i对象转成基本数值(自动拆箱) i.intValue() + 5; //加法运算完成后,再次装箱,把基本数值转成对象 Integer i = 1; Double d = 1;//错误的,1是int类型
包装类的一些API
把基本数据类型转换为字符串
int a = 10;
//String str = a;//错误的
//方式一:
String str = a + "";
//方式二:
String str = String.valueOf(a);
把字符串转为基本数据类型
int a = Integer.parseInt("整数的字符串");
double a = Double.parseDouble("小数的字符串");
boolean b = Boolean.parseBoolean("true或false");
转大小写
Character.toUpperCase('x');
Character.toLowerCase('X');
转进制
Integer.toBinaryString(int i) 
Integer.toHexString(int i)
Integer.toOctalString(int i)
包装类对象的缓存问题
| 包装类 | 缓存对象 | 
|---|---|
| Byte | -128~127 | 
| Short | -128~127 | 
| Integer | -128~127 | 
| Long | -128~127 | 
| Float | 没有 | 
| Double | 没有 | 
| Character | 0~127 | 
Integer i = 1;
Integer j = 1;
System.out.println(i == j);//true
Integer i = 128;
Integer j = 128;
System.out.println(i == j);//false
Integer i = new Integer(1);//新new的在堆中
Integer j = 1;//这个用的是缓冲的常量对象,在方法区
System.out.println(i == j);//false
Integer i = new Integer(1);//新new的在堆中
Integer j = new Integer(1);//另一个新new的在堆中
System.out.println(i == j);//false
Integer i = new Integer(1);
int j = 1;
System.out.println(i == j);//true,凡是和基本数据类型比较,都会先拆箱,按照基本数据类型的规则比较
Double d1 = 1.0;
Double d2 = 1.0;
System.out.println(d1==d2);//false 比较地址,没有缓存对象,每一个都是新new的
