java 6.异常处理

在局部内部类的方法中,如果调用局部内部类所声明的方法中的局部变量,要求此局部变量声明为final.

概述与异常体系结构:

Error:Java虚拟机无法解决的问题,如栈溢出、堆溢出。

Exception:编译时异常、运行时异常

java.lang.Throwable

        java.lang.Error:一般不编写针对性的代码处理

        java.lang.Exception:可以进行异常的处理

 

package j1;

public class ExceptionTest {
    //ArithmeticException
    public void test6(){
        int a=10;
        int b=0;
        System.out.println(a/b);
    }
    //InputMismatchException
    public void test5(){
        Scanner scanner=new Scanner(System.in);
        int score=scanner.nextInt();
        System.out.println(score);
        scanner.close();
    }
    //NumberFormatException
    public void test4(){
        String str="123";
        str="abc";
        int num=Integer.parseInt(str);
    }
    //ClassCastException类型转换异常
    public void test3(){
        Object obj=new Date();
        String str=(String) obj;
    }
    //数组角标越界ArrayIndexOutOfBoundsException
    public void test2(){
        int[] arr=new int[10];
        System.out.println(arr[10]);
        String str="abc";
        //StringIndexOutOfBoundsException
        System.out.println(str.charAt(3));
    }
    //空指针异常NullPointerException
    public void test1(){
        int[] arr=null;
        System.out.println(arr[3]);
        String str="abc";
        str=null;
        System.out.println(str.charAt(0));
    }
}

 异常处理的方式

try-catch-finally

throws+异常类型

抓抛模型

package j1;

import org.junit.Test;

public class ExceptionTest {
    //过程一:“抛”,程序在正常执行的过程中,一旦出现异常,就会在异常代码处生成一个对应异常类的对象,并将此对象抛出
    //一旦抛出对象之后,其后的代码就不再执行
    //过程二:“抓”,可以理解为异常处理的两种方式
    /*
    try-catch-finally的使用
    try{
        可能出现异常的代码
    }catch(异常类型1 变量名1){
         处理异常的方式1
    }catch(异常类型2 变量名2){
         处理异常的方式2
    }catch(异常类型3 变量名3){
         处理异常的方式3
    }
    finally{
        一定会执行的代码
    }
    注意:
    此结构可嵌套
    finally是可选的
    使用try将可能出现的异常包装起来,在执行过程中,一旦出现异常,就会生成一个对应异常类的对象,根据此对象的类型
    ,去catch中进行匹配。一旦try中的异常对象匹配到某一个catch时,就进入catch中进行异常的处理,处理完成后,跳出当前try-catch结构
    (没有finally的情况),继续执行其后面的代码
    catch中的异常类型如果没有子父类关系。则无所谓声明的位置
    catch中的异常类型如果满足子父类关系,则要求子类一定声明在父类的上面,否则会报错
    常用的异常对象的处理方式:① String getMessage()    ②printStackTrace()
    在try结构中声明的变量,出了try结构之后就不能被调用
    使用try-catch-finally处理编译时异常,使得程序在编译时不再报错,但是运行时仍可能报错
    相当于将一个编译时可能出现的异常,延迟到运行时出现
    开发中,由于运行时异常比较常见,所以我们通常就不针对运行时异常编写try-catch-finally。
    针对编译时异常,一定要考虑异常的处理。
     */
    @Test
        public void test1(){
            String str="123";
            str="abc";
            int num=0;
            try {
                num = Integer.parseInt(str);
                System.out.println("hello1");
            }catch(NumberFormatException e){
                System.out.println("出现数值转换异常");
                System.out.println(e.getMessage());
                e.printStackTrace();
            }
            System.out.println("hello2");
        }

}

try-catch-finally真正的将异常处理掉了

throws的方式只是将异常抛给了方法的调用者。并没有真正将异常处理掉

package j1;

import org.junit.Test;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

/*
throws+异常类型
写在方法的声明处。指明此方法执行时,可能会抛出的异常类型
一旦方法体执行时,出现异常,仍会在异常代码处生成一个异常类的对象,此对象满足throws后异常
类型时,会被抛出。
异常代码后续的代码不会被执行
 */
public class ExceptionTest {
    public static void main(String[] args){
        try{
            test2();
        }catch(IOException e){
            e.printStackTrace();
        }
        test3();
    }
    public static void test3(){
        try{
            test2();
        }catch(IOException e){
            e.printStackTrace();
        }
    }
    public static void test2()throws IOException{
        test1();
    }
    public static void test1()throws FileNotFoundException, IOException {
        File file=new File("hello.txt");
        FileInputStream fis=new FileInputStream(file);
        int date=fis.read();
        while(date!=-1){
            System.out.println((char)date);
            date=fis.read();
        }
        fis.close();
    }
}

如果父类中被重写的方法没有throws方式处理异常,则子类重写的方法也不能使用throws,意味着如果子类重写的方法中有异常,必须使用try-catch-finally处理。

执行的方法a中,先后又调用了另外的几个方法,这几个方法是递进关系执行的。建议这几个方法使用throw的方式处理,而执行的方法a可以考虑用try-catch-finally处理。 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值