美文网首页
Java篇-异常处理

Java篇-异常处理

作者: TianTianBaby223 | 来源:发表于2018-04-29 13:54 被阅读25次

一 : 常见的异常

  • 空指针异常 : NullpointerExcetion

对象已经为空,却还在发送消息.

public void test5() {
        Person p = new Person();
        p = null;
        System.out.println(p.toString());
    }
  • 数组下标越界的异常 : ArrayIndexOutofBoundsException
    public void test2() {
        int[] i = new int[10];
        System.err.println(i[10]);
  • 算数异常 ArithmeticExceptiom
public void test3() {
        int i = 10;
        System.out.println(i/0);
        
    }
  • 类型转换异常
public void test4() {
        Object obj = new Date();
        String str = (String)obj;
        
    }
  • 编译时异常

如不处理,编译不通过,对于编译时异常来说,必须要显示的进行处理
编译时异常,不是说有异常才处理,而是存在异常的隐患,必须在编译前,提示程序,万一出现异常,如何处理.

public void test6() throws Exception{
        
        FileInputStream fis = new FileInputStream(new File("hello.txt"));
        int b;
        while((b = fis.read())!= -1) {
            System.out.println((char)b);
        
          }
    }

二 : 异常的处理

Java提供异常处理的抓抛模型.
抛: 我们执行代码时,一旦出现异常,就会在异常的代码处生成一个对应的异常类型对象,(自动或者手动)并将此对象抛出,一旦抛出此类型异常对象,那么程序就终止执行,此类异常对象抛给方法的调用者.

抓:抓住上一步跑出来的异常类的对象,怎么样抓住,即为异常的处理方式.如果异常处理了,那么其后的代码继续执行.

  • try catch 处理异常

结构

* try{
 *  //可能出现异常的代码
 * }catch(Exception e1){
 *  //处理方式1
 * }catch(Exception e1){
 *  //处理方式2
 * }finally{
 *      // 一定要执行的代码
 * }

①try 内声明的变量,类似于局部变量,出了try{}语句,就不能被调用,i 的作用范围 只在try{}语句中

public void test3() {
        try {
            int i = 10;
            System.out.println(i/0);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            System.out.println("分母不能为零哦");
        }
        
    }

②finally是可选的,可以不写,如果写了,finally中存放的是一定会被执行的代码,不管try中,catch中是否仍有异常未被处理,以及是否有return语句

public static int method1() {
        try {
        System.out.println("你好");
            //还存在算数异常未处理,但最后 finally 代码还会被执行
         System.out.println(10/0);
            return 1;
            
        }catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
            return 3;
        }finally {
            System.out.println("最后的执行");
            
        }
    }   

测试

public static void main(String[] args) {
        int i = method1();
        System.out.println(i);
        
        System.out.println("继续哦");
        
    }

结果

你好
/ by zero
最后的执行
3
继续哦

③ catch语句内部是对异常对象的处理
---getMessage();获取打印
---printStackTrace();打印异常的堆栈信息

public void test3() {
        try {
            int i = 10;
            System.out.println(i/0);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            System.out.println(e.getMessage());
        }
        
    }

④可以有多个catch语句,try 中抛出的异常类对象从上往下去匹配catch中的异常类的类型,一旦满足就执行catch中的代码,执行完,就跳出其后的多条catch语句

@Test
        public void test4() {
            try {
                Object obj = new Date();
                String str = (String)obj;
            }catch (ClassCastException e) {
                // TODO: handle exception
                System.out.println("出现类型转换的异常了");
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            
            System.out.println("---的方式发第三方");

        }

执行结果

出现类型转换的异常了
---的方式发第三方

⑤若catch 中多个异常类型是并列关系,谁在上面谁在下都可以.
若catch中多个异常类型是包含关系,须将子类放在父类的上面,进行处理,否则报错

@Test
        public void test6() throws Exception{
            FileInputStream fis = null;
            try {
                 fis = new FileInputStream(new File("hello.txt"));
                int b;
                while((b = fis.read())!= -1) {
                    System.out.println((char)b);
                    
                }
                
            } catch (FileNotFoundException e) {

                //e.printStackTrace();
                //FileNotFoundException 是 IOException 的子类 小的写上面 大的写下面否则出错 
                System.out.println("文件找不到了");
            }catch(IOException e) {
                
                System.out.println(e.getMessage());
                System.out.println(10/0);
                
            }
        }

⑥try - catch 是可以嵌套的

public void test1() {
        Scanner s = new Scanner(System.in);
        try {
            int i = s.nextInt();
            System.out.println(i);
        } catch (InputMismatchException e) {
            // TODO: handle exception
            try {
                System.out.println(10/0);
            } catch (Exception e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            System.out.println("出现类型不匹配的异常了");
        }
        
    }
  • 在方法的声明处,显示的抛出该异常对象的类型

格式

public static void method2() throws  FileNotFoundException,IOException

当在方法内部出现异常的时候,会抛出一个异常类的对象,抛给方法的调用者,异常的对象可以逐层向上抛,直至main中,在向上抛的过程中,还可以用try-catch-finially进行处理.

public class TestException {
    public static void main(String[] args) {
                  //处理抛出的异常
        try {
            TestException.method2();
        } catch (FileNotFoundException e) {
            
            System.out.println(e.getMessage());
        } catch (IOException e) {
            
            e.printStackTrace();
        }
        
    }
    
    //再往上面抛
public static void method2() throws FileNotFoundException,IOException{
        
     method1();
        
    }
    
    //异常抛给方法的调用者
    public static void method1() throws FileNotFoundException,IOException {
        FileInputStream fis = new FileInputStream(new File("TZ.txt"));
        int b;
        while((b = fis.read()) != -1) {
            System.out.println((char)b);

        }
        fis.close();
    }
}
  • throw 手动抛出异常

抛出的异常类型,若是RuntimeException 可以不显示的处理
若是一个Exception,必须要显示的处理.

定义一个Student类 throw Exception

class Student{
    
    int id;
    public void regist(int id) {
        if(id > 0) {
            this.id = id;
        }else {

                      //爆红 因为这个是继承Exception 需要显示处理
            throw new TZException("传入的学号有误");
        }   
    }
}

定义一个Student类 throw RuntimeException

class Student{
    
    int id;
    public void regist(int id){
        if(id > 0) {
            this.id = id;
        }else {
                      //不爆红
            throw new RuntimeException("你传入类型不对");
        }   
    }
}

调用

public class TestStudent {
    public static void main(String[] args) {
        Student s = new Student();
        try {
            s.regist(-12);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }
}
  • 子类重写的父类的方法,其抛出的异常类型只能是被重写的方法的异常类的子类或异常类型一样.

解释 : 编译看左,运行看右(多态),假如 重写的异常大于父类异常,编译时候按照父类的异常,而真正运行的时候则是子类的异常,由于子类的异常大于父类的异常可能会出错,而且Java 也不允许这样做.

public class TestOverride {
    public static void main(String[] args) {
        A a = new B();
        try {
            a.menthod1();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

class A {
    public void menthod1() throws IOException{
        
        
    }
}

class B extends A{
    public void menthod1() throws FileNotFoundException{
        
    }
}

三 : 自定义异常

自定义的异常类继承现有的异常类,且提供一个方法号,提供几个重载的构造器.

public class TZException extends Exception{
    static final long serialVersionUID = -7034897190745766939L;
   
    public TZException() {
        
    }
    public  TZException(String msg) {
        super(msg);
        
    }
}

相关文章

  • Java篇-异常处理

    一 : 常见的异常 空指针异常 : NullpointerExcetion 对象已经为空,却还在发送消息. 数组下...

  • Java & Groovy & Scala & Kotlin -

    Overview 本章主要介绍各语言中异常声明和处理方式。 Java 篇 分类 在 Java 中异常是种特殊对象,...

  • Java- 异常处理

    Java- 异常处理 异常处理能够使一个方法给它的调用者抛出一个异常。 Java异常是派生自 java.lang....

  • JNI异常

    JNI异常 JNI允许native方法引发任意Java异常。native代码还可以处理未解决的Java异常。未处理...

  • 碰见异常 你是选就地正法 还是甩锅大法

    java零基础入门-高级特性篇(九) 异常 中 上一节讲到了检查异常,这种必须处理的异常到底该怎么处理呢?通常的处...

  • Java 异常分析

    本文是对以下内容的分析: Java异常设计 Java 异常分类 Java异常可以告诉什么问题 Java异常处理最佳...

  • Java异常处理-检查性异常、非检查性异常、Error

    一、Java异常处理详解 Java异常处理-笔记中的@doublefan讲解得非常通熟易懂 二、检查型异常和非检查...

  • java异常---异常处理

    一、重点知识 IDEA创建配置文件file目录下选择Resource Bundle,直接输入文件名创建就好了 记得...

  • Java基础系列-Exception异常处理

    原创文章,转载请标注出处:《Java基础系列-Exception异常处理》 一、概述 Java代码中的异常处理是非...

  • Java 学习基础篇 ---- Java 异常处理

    一、错误处理 (一) java的异常 1、Java 使用异常来表示错误:(1)异常是一种类 class;(2)本身...

网友评论

      本文标题:Java篇-异常处理

      本文链接:https://www.haomeiwen.com/subject/cdqakftx.html