异常

异常
1.概念:
程序在编译运行过程中遇到的种种不正常的现象
Java通过Throwable类来描述各种不同的异常,Java中的异常都是面向对象的,所有的异常都是Throwable的子类

2.分类
Throwable的子类:Exception Error
Error:大多数与代码编写者没有关系,表示当代码运行时,JVM出现的问题,例如:StackOverFlow(栈溢出)
Exception:程序能够捕获并且处理的异常
CheckedException:编译时异常,这种异常必须要处理,否则编译都无法通过
RuntimeException:运行时异常,编译可以通过,但是在程序运行的时候被发现


3.处理异常
处理机制:抛出异常,捕捉异常


a.捕捉异常:try catch
语法:
try{
//可能会有异常的代码
}
catch(Exception e){

//捕捉并处理try代码块中出现的异常
}
说明:关键词try后面的代码块中写可能会有异常的代码,这块区域被称为监控区域,
如果在try中检测到了异常,程序在运行的时候系统会试图匹配对应的catch代码块
如果匹配到了对应catch代码块,则运行并处理异常,结束之后整个try-catch语句结束


b.抛出异常throw throws
throw :抛出一个异常的对象,调用这个对象所在的方法的时候就会出现这种异常
throws:声明在方法定义的时候,用来表示这个方法可能会抛出的异常


补充问题:
1.多个catch
2.e1.printStackTrace();

//返回异常的消息信息
System.out.println(e1.getMessage());//String

3.如果在try中出现多个异常的时候,只处理位于最前面的异常,但是后面的异常不会对程序产生影响

4、自定义异常
在实际项目开发中,如果系统提供的异常不能够满足实际的需求的话,就需要自定义异常

实现方式:
编译时异常:继承自Exception类,
运行时异常:继承自RuntimeException

---------------------------------------------------------------------------------------------------------------------------------

如果程序中遇到异常,并且这个异常没有被处理的话,那么这个程序会被终止异常后面的代码也不会继续执行

 1 class ExceptionTypeDemo 
 2 {
 3     public static void main(String[] args) 
 4     {
 5         //常见的异常演示
 6         //1.NullPointerException:空指针异常(运行时异常)
 7         String  str = null;
 8         //System.out.println(str.isEmpty());//java.lang.NullPointerException
 9 
10         //如果程序中遇到了异常,并且这个异常没有被处理的话,那么程序会被终止
11         //System.out.println("hello world");
12 
13         //2.ArrayIndexOutOfBoundsException:数组越界(运行时异常)
14         int[] arr = {1,2,3,4};
15         //System.out.println(arr[4]);//java.lang.ArrayIndexOutOfBoundsException
16 
17         //3.InputMismatchException:类型不匹配(运行时异常)
18         //int num = new java.util.Scanner(System.in).nextInt();//java.util.InputMismatchException
19 
20         //4.NumberFormatException:数据格式化异常(运行时异常)
21         //int num1 = Integer.parseInt("abc123");//java.lang.NumberFormatException
22 
23         //5.ParseException:解析异常(编译时异常)
24         //new java.text.SimpleDateFormat("yyyy.MM.dd HH:mm:ss").parse("2013.1.2 12:30:00");
25         // 错误: 未报告的异常错误ParseException; 必须对其进行捕获或声明以便抛出
26     
27         
28         //6.ArithmeticException:数学异常(运行时异常)
29         int num2 = 10 / 0;//java.lang.ArithmeticException
30 
31     }
32 }

1.NullPointerException:空指针异常(运行时异常)

例如:

1 punlic class NullPointerExceptionDemo{
2          public static void main(String[] args){
3             //空指针异常NullPointerException
4             String str = null;
5             System.out.println(str.isEmpty());//异常报错为:java.lang.NullPointerException
6  
7             System.out.println("hello world");//出现异常这一句不会被执行
8   }
9 }

Exception in thread "main" java.lang.NullPointerException:       

所谓空指针错误是:你叫某个人去做一件事,但这个人并不存在,所以没人干这个事情,那这事就会出差错。

2.ArrayIndexOutOfBoundsException:数组越界(运行时异常)
1 class ExceptionTypeDemo 
2 {
3     public static void main(String[] args) 
4     {
5 //2.ArrayIndexOutOfBoundsException:数组越界(运行时异常)
6         int[] arr = {1,2,3,4};
7         //System.out.println(arr[4]);//java.lang.ArrayIndexOutOfBoundsException
8     }
9 }
3.InputMismatchException:类型不匹配(运行时异常)

4.NumberFormatException:数据格式化异常(运行时异常)
5.ParseException:解析异常(编译时异常)
6.ArithmeticException:数学异常(运行时异常)

原文地址:https://www.cnblogs.com/lidar/p/7760652.html