美文网首页
Java异常处理

Java异常处理

作者: 筱笑 | 来源:发表于2017-07-14 14:34 被阅读17次

一、捕获异常

异常是导致程序中断运行的一种指令流,如果不对异常进行正确处理,则可能导致程序的中断执行,造成不必要的损失。

1、异常范例

【示例-1】空指针异常

代码

class Exc{
    int i = 10;
}
public class Test49 {
    public static void main(String[] args) {
        Exc c = new Exc();
        System.out.println(c.i);
        Exc b = null;
        System.out.println(b.i); //此部分会报空指针异常
    }
}
结果:
10
Exception in thread "main" java.lang.NullPointerException
    at cn.sec.ch01.Test49.main(Test49.java:16)

【示例-2】算数异常

代码

public class Test49 {
  public static void main(String[] args) {
      int a = 10;
      int b = 0;
      int temp = a/b;
      System.out.println(temp);
  }
}
异常结果:
Exception in thread "main" java.lang.ArithmeticException: / by zero
  at cn.sec.ch01.Test49.main(Test49.java:16)

2、处理异常

异常格式:
try{
异常语句
}catch(Exception e){
}finally{
一定会执行的代码
}
示例
代码

public class Test49 {
    public static void main(String[] args) {
        
        int a = 10;
        int b = 0;
        int temp = 0;
        try {
            temp = a/b;
        } catch (Exception e) {
            System.out.println(e);
        }
        
        System.out.println(temp);
    }
}
结果:
java.lang.ArithmeticException: / by zero
0

二、常见异常

  • 数组越界异常:ArrayIndexOutOfBoundsException
  • 数字格式化异常:NumberFormatException
  • 算数异常:ArithmeticException
  • 空指针异常:NullPointerException
代码一
class Exam{
    int a = 10;
    int b = 0;  
}
public class Test50 {

    public static void main(String[] args) {
        Exam e = null; //创建对象
        e = new Exam();//实例化
        
        int temp = 0;
        try {
            temp = e.a/e.b;
            System.out.println(temp);
        } catch (NullPointerException e2) {
            System.out.println("空指针异常:"+e2);
        } catch (ArithmeticException e2) {
            System.out.println("算数异常:"+e2);
        }finally {
            System.out.println("程序退出");
        }
    }
}
结果:
算数异常:java.lang.ArithmeticException: / by zero
程序退出

代码二
class Exam{
    int a = 10;
    int b = 10;
}
public class Test50 {

    public static void main(String[] args) {
        Exam e = null; //创建对象
//      e = new Exam();//实例化
        
        int temp = 0;
        try {
            temp = e.a/e.b;
            System.out.println(temp);
        } catch (NullPointerException e2) {
            System.out.println("空指针异常:"+e2);
        } catch (ArithmeticException e2) {
            System.out.println("算数异常:"+e2);
        }finally {
            System.out.println("程序退出");
        }
    }
}
结果:
空指针异常:java.lang.NullPointerException
程序退出

代码三
class Exam{
    int a = 10;
    int b = 10;
}
public class Test50 {

    public static void main(String[] args) {
        Exam e = null; //创建对象
        e = new Exam();//实例化
        
        int temp = 0;
        try {
            temp = e.a/e.b;
            System.out.println(temp);
        } catch (NullPointerException e2) {
            System.out.println("空指针异常:"+e2);
        } catch (ArithmeticException e2) {
            System.out.println("算数异常:"+e2);
        }finally {
            System.out.println("程序退出");
        }
    }
}
结果:
1
程序退出

三、throws关键字

  • 在定义一个方法的时候可以使用throws关键字声明,使用throws声明的方法表示此方法不处理异常,抛给方法的调用者处理。如果是主方法抛出异常,就是JVM进行处理
  • 格式:public void tell() throws Exception{}
示例
代码

public class Test51 {
    public static void main(String[] args) {
        try {
            tell(10, 0);
        } catch (Exception e) {
            System.out.println(e);
        }
    }
    
    public static void tell(int i,int j) throws ArithmeticException{
        int temp = 0;
        temp = i/j;
        System.out.println(temp);
    }
}
结果:
java.lang.ArithmeticException: / by zero

四、throw关键字

throw关键字抛出一个异常,抛出的时候直接抛出异常类的实例化对象即可。

示例
代码

public class Test52 {
    public static void main(String[] args) {
        try {
            throw new Exception("实例化异常对象");
        } catch (Exception e) {
            System.out.println(e);
        }
    }
}
结果:
java.lang.Exception: 实例化异常对象

五、自定义异常

自定义异常直接集成Exception就可以完成自定义异常类。

示例
代码

package cn.sec.ch01;

class MyException extends Exception{
    public MyException (String msg) {
        super(msg);
    }
}
public class Test53 {
    public static void main(String[] args) {
        try {
            throw new MyException("自定义异常");
        } catch (MyException e) {
            System.out.println(e);
        }
    }
}
结果:
cn.sec.ch01.MyException: 自定义异常

相关文章

  • Java- 异常处理

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

  • JNI异常

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

  • Java 异常分析

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

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

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

  • java异常---异常处理

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

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

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

  • 04- JAVA异常

    在JAVA中异常就是一个类,产生异常就是创建异常对象并抛出一个异常对象。JAVA处理异常的方式是中断处理。参考:j...

  • Kotlin2.5异常

    Kotlin的异常处理和Java以及其他许多语言的处理方式相似,异常处理语句的基本形式和Java类似,抛出异常的方...

  • Java自学-异常处理 处理

    Java的异常处理办法 try catch throws 异常处理常见手段: try catch finally ...

  • Java 异常面试问题与解答

    Java 提供了一种健壮且面向对象的方法来处理称为 Java异常处理的异常情况。 1. Java中的异常是什么? ...

网友评论

      本文标题:Java异常处理

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