本文详细介绍了JAVA中的常见异常,以及异常的处理办法

  1. Throwable类:是Java中所有错误和异常的父类

Throwable类有两个子类:一个是错误类 Error  一个是异常类 Exception

错误是指:类似虚拟机断电,电脑断电等在程序中无法解决的问题

异常则是指:程序中执行结果与预期结果不一致  

异常有分为编译器异常(Exception)和运行期异常(RuntimeException)

编译器异常是指:在程序编写时出现的异常,编译器会对编译期异常做出提示

运行期异常是指:在程序编译完成,生产.class文件,运行时产生的异常  预期结果与执行结果不一致

 

下面来列举五个常见的运行期异常:

  1.   数组角标(索引)越界异常:ArraayIndexOutOfBoundsException

  代码示例:

  1. 1 package com.niubi;
  2. 2
  3. 3 public class Demo6 {
  4. 4
  5. 5 public static void main(String[] args) {
  6. 6 int[] array=new int[5];
  7. 7 System.out.println(array[6]); //访问的数组索引越界,属于运行期异常,编译器不会有异常提示
  8. 8
  9. 9 }
  10. 10
  11. 11 }

 

 这个代码是运行期异常,运行期的异常提示如下:在异常提示中,提示代码的第7行有错误

  2.  空指针异常:NullPointerException

  代码示例:

  1. 1 package com.niubi;
  2. 2
  3. 3 public class Demo7 {
  4. 4
  5. 5 public static void main(String[] args) {
  6. 6 Object object=new Object();
  7. 7 object=null; //object对象被赋值null(空),所以无法调用hashCode方法,为空指针异常
  8. 8 System.out.println(object.hashCode());
  9. 9
  10. 10 }
  11. 11
  12. 12 }

 

这个代码是运行期异常,异常提示如下

 

  3.  类型转换异常:ClassCastException

  代码示例:

  1. 1 package com.niubi;
  2. 2
  3. 3 public class Demo8 {
  4. 4
  5. 5 public static void main(String[] args) {
  6. 6 Object object=new Object();
  7. 7 String string=(String)object; //Object为String的父类,不能向下转型,所以会出现类型转换异常
  8. 8 System.out.println(string);
  9. 9
  10. 10 }
  11. 11
  12. 12 }

 

类型转换异常的异常提示如下:

 

  4.输入不匹配异常:InputMismatchException

  代码示例:

  1. 1 package com.niubi;
  2. 2
  3. 3 import java.util.Scanner;
  4. 4
  5. 5 public class Demo9 {
  6. 6
  7. 7 public static void main(String[] args) {
  8. 8 Scanner sc=new Scanner(System.in);
  9. 9 System.out.println("输入数字");
  10. 10 int input=sc.nextInt(); //要求输入数字,但是输入的是字母
  11. 11 System.out.println(input);
  12. 12
  13. 13 }
  14. 14
  15. 15 }

 

异常提示如下:

 

  5.字符串索引越界:StringIndexOutOfBoundsException

  代码示例:

  1. 1 package com.niubi;
  2. 2
  3. 3 public class Demo10 {
  4. 4
  5. 5 public static void main(String[] args) {
  6. 6 String string=new String();
  7. 7 string="abc";
  8. 8 System.out.println(string.charAt(4)); //string只有三个元素,却要访问string的第四个元素,会报字符串索引越界
  9. 9
  10. 10 }
  11. 11
  12. 12 }

 

异常提示如下:

 

学习,了解异常的好处在于:可以提高程序的健壮性。

那么面对异常,我们该如何处理异常呢?

  异常的处理有两种方式

  1.异常的声明(这种方法不修改异常的代码,只是提示有异常)

  异常声明的格式如下:  在方法后边加上  throw 异常名

  2.异常的捕获

  try—catch—finally

  3.JAVA中异常的产生与运行原理:

  产生:如果程序在某个位置发生了异常,程序会在该位置将异常信息封装成异常对象,并且抛给调用者,由调用者处理,如果调用者无法处理,那么异常对象会被继续上抛,直至被抛到JVM(虚拟机)为止。

  JVM(虚拟机)并不会对异常进行处理,JVM会将异常信息显示在控制台上。

需要注意的是:发生异常的地方,它后面的程序不会被执行。

代码示例:

  1. 1 public class 异常的细节问题 {
  2. 2 public static void main(String[] args) throws ParseException {
  3. 3 /* fun();
  4. 4 System.out.println("bbbbb");*/
  5. 5 fun1();
  6. 6 }
  7. 7 public static void fun() {
  8. 8 Scanner sc=new Scanner(System.in);
  9. 9 System.out.println(5/sc.nextInt());
  10. 10 System.out.println("aaaaaaa");
  11. 11 }
  12. 12 // 异常的声明
  13. 13 public static void fun1() throws ParseException {
  14. 14 SimpleDateFormat sf=new SimpleDateFormat("yyyy");
  15. 15 Date st = sf.parse("aas");
  16. 16 System.out.println(st);
  17. 17 }
  18. 18 }

 

 

  异常的处理

  1.   try–catch–finally

  格式①  try–catch

  try{

   可能会发生异常的代码

   }

  catch(异常的声明){   //异常声明的格式:异常的数据类型:异常的变量名称

    进行异常处理的代码

    }

   catch(异常的声明){   //异常声明的格式:异常的数据类型:异常的变量名称

    进行异常处理的代码

    }

    注意:在多catch的情况下:如果各个异常没有关联,可以任意顺序,但是有子父类的关系,必须子类在前,父类在后

  总结:try中写可能会出现异常的代码,catch中写对于异常的处理的代码,如果异常并没有发生,则不执行catch中的代码,如果发生异常,则会进入catch处理异常,并且try中发生异常之后的代码不会被执行。

  

  try—catch—finally

  try{

  }

  catch{

  }

  finally{

  }

无论异常发生与否,finally中的代码都会被执行

代码示例:

  1. 1 public class Try_catch_finally {
  2. 2 public static void main(String[] args) {
  3. 3 Scanner sc=new Scanner(System.in);
  4. 4 try {
  5. 5 System.out.println(" 请输入您要进行除法的操作 ");
  6. 6 int i=5/sc.nextInt();
  7. 7 System.out.println(i);
  8. 8 }catch(ArithmeticException e) {
  9. 9 // System.out.println(e.getMessage());
  10. 10 // e.printStackTrace();
  11. 11 // System.out.println(e.toString());
  12. 12 }
  13. 13 }
  14. 14 }
  15. 15 public class Try_catch_finally 异常的处理 {
  16. 16 public static void main(String[] args) {
  17. 17 Scanner sc=new Scanner(System.in);
  18. 18 while (true) {
  19. 19 try {
  20. 20 System.out.println(" 请输入您要进行除法的操作 ");
  21. 21 int i = 5 / sc.nextInt();
  22. 22 System.out.println(i);
  23. 23 // 循环结束
  24. 24 break;
  25. 25 } catch (ArithmeticException e) {
  26. 26 System.out.println(" 除 0 异常,除数不能为 0 ,请重新输入 ");
  27. 27 // 多个异常可以并列
  28. 28 } catch (InputMismatchException|ClassCastException e) {
  29. 29 System.out.println(" 不能输入字符,只能是数字 ");
  30. 30 sc=new Scanner(System.in);
  31. 31 }
  32. 32 }
  33. 33 }
  34. 34 }

 

自定义异常:

继承 Exception  获取 RuntimeException , 但是如果需要传递异常信息,需要调用父类有参构造

 

代码示例:

  1. 1 public class Topic1 {
  2. 2 public static void main(String[] args) {
  3. 3 Student s=new Student();
  4. 4 int age=150;
  5. 5 try {
  6. 6 // int i=5/0;
  7. 7 s.setAge(age);
  8. 8 } catch (AgeException e) {
  9. 9 // TODO Auto-generated catch block
  10. 10 System.out.println("=====");
  11. 11 // e.printStackTrace();
  12. 12 System.out.println(e.getMessage());
  13. 13 }
  14. 14 }
  15. 15 }
  16. 16 class Student {
  17. 17 String name;
  18. 18 int age;
  19. 19 public void setAge(int age) throws AgeException{
  20. 20 if(age>120||age<0) {
  21. 21 // 抛出异常对象
  22. 22 throw new AgeException(" 年龄不合法 ");
  23. 23 }
  24. 24 this.age=age;
  25. 25 }
  26. 26 }
  27. 27 // 自定义异常 : 继承 (具有父类的特点 ) Exception 或 RuntimeException
  28. 28 class AgeException extends Exception{
  29. 29 public AgeException(String msg) {
  30. 30 // 必须通过调用父类的有参构造来传递异常信息。
  31. 31 super(msg);
  32. 32 }
  33. 33 }

 

版权声明:本文为ywzbky原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/ywzbky/p/10685627.html