JAVA中异常详解
本文详细介绍了JAVA中的常见异常,以及异常的处理办法
- Throwable类:是Java中所有错误和异常的父类
Throwable类有两个子类:一个是错误类 Error 一个是异常类 Exception
错误是指:类似虚拟机断电,电脑断电等在程序中无法解决的问题
异常则是指:程序中执行结果与预期结果不一致
异常有分为编译器异常(Exception)和运行期异常(RuntimeException)
编译器异常是指:在程序编写时出现的异常,编译器会对编译期异常做出提示
运行期异常是指:在程序编译完成,生产.class文件,运行时产生的异常 预期结果与执行结果不一致
下面来列举五个常见的运行期异常:
- 数组角标(索引)越界异常:ArraayIndexOutOfBoundsException
代码示例:
- 1 package com.niubi;
- 2
- 3 public class Demo6 {
- 4
- 5 public static void main(String[] args) {
- 6 int[] array=new int[5];
- 7 System.out.println(array[6]); //访问的数组索引越界,属于运行期异常,编译器不会有异常提示
- 8
- 9 }
- 10
- 11 }
这个代码是运行期异常,运行期的异常提示如下:在异常提示中,提示代码的第7行有错误
2. 空指针异常:NullPointerException
代码示例:
- 1 package com.niubi;
- 2
- 3 public class Demo7 {
- 4
- 5 public static void main(String[] args) {
- 6 Object object=new Object();
- 7 object=null; //object对象被赋值null(空),所以无法调用hashCode方法,为空指针异常
- 8 System.out.println(object.hashCode());
- 9
- 10 }
- 11
- 12 }
这个代码是运行期异常,异常提示如下
3. 类型转换异常:ClassCastException
代码示例:
- 1 package com.niubi;
- 2
- 3 public class Demo8 {
- 4
- 5 public static void main(String[] args) {
- 6 Object object=new Object();
- 7 String string=(String)object; //Object为String的父类,不能向下转型,所以会出现类型转换异常
- 8 System.out.println(string);
- 9
- 10 }
- 11
- 12 }
类型转换异常的异常提示如下:
4.输入不匹配异常:InputMismatchException
代码示例:
- 1 package com.niubi;
- 2
- 3 import java.util.Scanner;
- 4
- 5 public class Demo9 {
- 6
- 7 public static void main(String[] args) {
- 8 Scanner sc=new Scanner(System.in);
- 9 System.out.println("输入数字");
- 10 int input=sc.nextInt(); //要求输入数字,但是输入的是字母
- 11 System.out.println(input);
- 12
- 13 }
- 14
- 15 }
异常提示如下:
5.字符串索引越界:StringIndexOutOfBoundsException
代码示例:
- 1 package com.niubi;
- 2
- 3 public class Demo10 {
- 4
- 5 public static void main(String[] args) {
- 6 String string=new String();
- 7 string="abc";
- 8 System.out.println(string.charAt(4)); //string只有三个元素,却要访问string的第四个元素,会报字符串索引越界
- 9
- 10 }
- 11
- 12 }
异常提示如下:
学习,了解异常的好处在于:可以提高程序的健壮性。
那么面对异常,我们该如何处理异常呢?
异常的处理有两种方式
1.异常的声明(这种方法不修改异常的代码,只是提示有异常)
异常声明的格式如下: 在方法后边加上 throw 异常名
2.异常的捕获
try—catch—finally
3.JAVA中异常的产生与运行原理:
产生:如果程序在某个位置发生了异常,程序会在该位置将异常信息封装成异常对象,并且抛给调用者,由调用者处理,如果调用者无法处理,那么异常对象会被继续上抛,直至被抛到JVM(虚拟机)为止。
JVM(虚拟机)并不会对异常进行处理,JVM会将异常信息显示在控制台上。
需要注意的是:发生异常的地方,它后面的程序不会被执行。
代码示例:
- 1 public class 异常的细节问题 {
- 2 public static void main(String[] args) throws ParseException {
- 3 /* fun();
- 4 System.out.println("bbbbb");*/
- 5 fun1();
- 6 }
- 7 public static void fun() {
- 8 Scanner sc=new Scanner(System.in);
- 9 System.out.println(5/sc.nextInt());
- 10 System.out.println("aaaaaaa");
- 11 }
- 12 // 异常的声明
- 13 public static void fun1() throws ParseException {
- 14 SimpleDateFormat sf=new SimpleDateFormat("yyyy");
- 15 Date st = sf.parse("aas");
- 16 System.out.println(st);
- 17 }
- 18 }
异常的处理
1. try–catch–finally
格式① try–catch
try{
可能会发生异常的代码
}
catch(异常的声明){ //异常声明的格式:异常的数据类型:异常的变量名称
进行异常处理的代码
}
catch(异常的声明){ //异常声明的格式:异常的数据类型:异常的变量名称
进行异常处理的代码
}
注意:在多catch的情况下:如果各个异常没有关联,可以任意顺序,但是有子父类的关系,必须子类在前,父类在后
总结:try中写可能会出现异常的代码,catch中写对于异常的处理的代码,如果异常并没有发生,则不执行catch中的代码,如果发生异常,则会进入catch处理异常,并且try中发生异常之后的代码不会被执行。
try—catch—finally
try{
}
catch{
}
finally{
}
无论异常发生与否,finally中的代码都会被执行
代码示例:
- 1 public class Try_catch_finally {
- 2 public static void main(String[] args) {
- 3 Scanner sc=new Scanner(System.in);
- 4 try {
- 5 System.out.println(" 请输入您要进行除法的操作 ");
- 6 int i=5/sc.nextInt();
- 7 System.out.println(i);
- 8 }catch(ArithmeticException e) {
- 9 // System.out.println(e.getMessage());
- 10 // e.printStackTrace();
- 11 // System.out.println(e.toString());
- 12 }
- 13 }
- 14 }
- 15 public class Try_catch_finally 异常的处理 {
- 16 public static void main(String[] args) {
- 17 Scanner sc=new Scanner(System.in);
- 18 while (true) {
- 19 try {
- 20 System.out.println(" 请输入您要进行除法的操作 ");
- 21 int i = 5 / sc.nextInt();
- 22 System.out.println(i);
- 23 // 循环结束
- 24 break;
- 25 } catch (ArithmeticException e) {
- 26 System.out.println(" 除 0 异常,除数不能为 0 ,请重新输入 ");
- 27 // 多个异常可以并列
- 28 } catch (InputMismatchException|ClassCastException e) {
- 29 System.out.println(" 不能输入字符,只能是数字 ");
- 30 sc=new Scanner(System.in);
- 31 }
- 32 }
- 33 }
- 34 }
自定义异常:
继承 Exception 获取 RuntimeException , 但是如果需要传递异常信息,需要调用父类有参构造
代码示例:
- 1 public class Topic1 {
- 2 public static void main(String[] args) {
- 3 Student s=new Student();
- 4 int age=150;
- 5 try {
- 6 // int i=5/0;
- 7 s.setAge(age);
- 8 } catch (AgeException e) {
- 9 // TODO Auto-generated catch block
- 10 System.out.println("=====");
- 11 // e.printStackTrace();
- 12 System.out.println(e.getMessage());
- 13 }
- 14 }
- 15 }
- 16 class Student {
- 17 String name;
- 18 int age;
- 19 public void setAge(int age) throws AgeException{
- 20 if(age>120||age<0) {
- 21 // 抛出异常对象
- 22 throw new AgeException(" 年龄不合法 ");
- 23 }
- 24 this.age=age;
- 25 }
- 26 }
- 27 // 自定义异常 : 继承 (具有父类的特点 ) Exception 或 RuntimeException
- 28 class AgeException extends Exception{
- 29 public AgeException(String msg) {
- 30 // 必须通过调用父类的有参构造来传递异常信息。
- 31 super(msg);
- 32 }
- 33 }