异常:
程序在运行的过程中出现的例外状况,这样的例外状况可以导致我们的运行终止。
java语言中这种例外的情况分为两类:1.Exception 2.error
Exception一般由程序引起,开发人员根据异常的类型可以处理,让我们的程序可以恢复到正常的状态
error一般由jvm引起,比如jvm内存耗尽,出现错误程序无法运行,而开发人员无法处理
error和Exception在java中都继承了Throwable
Exception异常:1.运行时的异常(未检出的异常):是程序逻辑引起的,不经常出现,编译器不进行检查,在程序运行的时候发生
java.lang.RuntimeException 及其子类
2.编译期异常(检查异常):不是逻辑引起的,而是由于程序员粗心造成的,比如传输文件的 时候,地址输入错误,这个文件就传输失败了,所以出现这种异常必须处理 除了java.lang.RuntimeException及其子类之外的异常
常见的运行时的异常:
java.lang.ArraysIndexOutOfBoundsException 数组越界
访问数组元素的时候,索引超过了数组的下标范围
null在java中的含义:空引用
java.lang.NullPointerException 空指针异常
出现的原因:当应用变量为null 时就调用了这个对象的成员变量或成员方法时发生
java语言提供了异常处理机制
1.抛出异常 throws
当前方法中出现了某些异常而当前方法体无法处理
【修饰符】 返回值类型 方法名(【参数类型】 参数名)throws 异常类型{
//可以产生异常的方法体
}
例子:
package exceptionTest; import java.io.IOException; import java.text.ParseException; import java.text.SimpleDateFormat; public class ThrowsTest { public static void main(String[] args) throws ParseException, IOException { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd "); sdf.parse("2013-03-03 12:12:30"); int s = System.in.read(); System.out.println(s); String ss = null; System.out.println(ss.charAt(0)); } }
捕捉异常 try catch finally
try{
//可能出现异常的代码以及他的相关代码
}catch(【异常类型】 【 对象名】){
//捕捉到这种类型的异常时需要执行的代码
e.printStrackTrace();
}
例子:
package exceptionTest; import java.io.IOException; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class TryCatcTest { public static void main(String[] args) { Date d = null; SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); try { d = sdf.parse("2019-10-10 12:12:12"); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ System.out.println(d); System.out.println("you die this time!!!"); } try { System.in.read(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } try{ sdf.parse("2019-12-12"); System.in.read(); }catch(Exception e){ e.printStackTrace(); }finally{ System.out.println("24578ertuisdfgj"); } } }
关于catch操作:
1.一个try可以跟0到多个catch,不同的catch块可以对不同的异常进行处理
2.一个try{}中出现异常,那么异常匹配就从第一个catch由上到下依次匹配类型
3.try之后必须跟catch语句或者finally语句 如果try之后已经有catch语句了 那么finally语句一定要放在最后
注:finally语句内语句必须执行,甚至包括try中的return语句也无法中断
自定义异常
自定义异常类继承 java.lang.RuntimeException 或者其他子类 那么这个类定义的异常就是运行时异常
否则,就是编译期异常。
但是一般情况下,我们一般定义为运行时异常
throw和thorws 的区别:
throws 一般放在方法声明部分,用于声明当前方法可能抛出的某些类型的异常
throw 放在方法体中,用于抛出一个异常对象
例子:
package lx; public class Student { private String name; private int age; // Exception AgeLT0Exception = new Exception(); public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) throws AgeGT150Exception{ if(age<=0){ throw new AgeLT0Exception(); }else if(age>150){ throw new AgeGT150Exception(); } this.age = age; } class AgeGT150Exception extends Exception{ public AgeGT150Exception() { super(); // TODO Auto-generated constructor stub } public AgeGT150Exception(String message, Throwable cause) { super(message, cause); // TODO Auto-generated constructor stub } public AgeGT150Exception(String message) { super(message); // TODO Auto-generated constructor stub } public AgeGT150Exception(Throwable cause) { super(cause); // TODO Auto-generated constructor stub } } class AgeLT0Exception extends RuntimeException { public AgeLT0Exception() { super(); // TODO Auto-generated constructor stub } public AgeLT0Exception(String message, Throwable cause) { super(message, cause); // TODO Auto-generated constructor stub } public AgeLT0Exception(String message) { super(message); // TODO Auto-generated constructor stub } public AgeLT0Exception(Throwable cause) { super(cause); // TODO Auto-generated constructor stub } } public static void main(String[] args) { // TODO Auto-generated method stub Student s = new Student(); try { s.setAge(-1); } catch (AgeLT0Exception e1) { // TODO Auto-generated catch block e1.printStackTrace(); } catch (AgeGT150Exception e1) { // TODO Auto-generated catch block e1.printStackTrace(); } try { s.setAge(160); } catch (AgeLT0Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (AgeGT150Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }