1.异常基本概念
异常模拟的是现实世界中不正常的事件
java中,采用类的形式去模拟异常
类是可以创建对象的 NullPointException e = 0x1234; e是引用类型,e中保存的内存地址指向堆中的“对象”
对象就是NullPointException类型,表示真实存的异常事件
NullPointException代表的是一类异常
2.异常机制的作用
程序发生异常事件之后为我们输出详细的信息,程序员通过这个信息,处理程序,使程序更加健壮
异常的层次结构
异常的继承结构图
处理异常有两种方法
1.声明抛出 throws
public class Demo01 {
public static void main(String[] args) {
FileInputStream file = new FileInputStream("c:/ah.txt");
}
}
2.捕捉
public class Demo01 {
public static void main(String[] args) {
try {
FileInputStream file = new FileInputStream("c:/ah.txt");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
详解throws(处理不了才用throws)
public class Dmoe02 {
public static void main(String[] args) throws FileNotFoundException {//使用throws处理异常不是真正的处理异常,而是推卸责任
m1(); //throws(向上抛) ----谁调用就抛给谁
//如果m1方法出现了异常,就向上抛给JVM,JVM就会退出,不会执行下面的
try {
m1();//真正处理
}catch(FileNotFoundException e) {
System.out.println("执行");
}
}
public static void m1() throws FileNotFoundException {
m2();
}
public static void m2() throws FileNotFoundException {
m3();
}
public static void m3() throws FileNotFoundException {
new FileInputStream("c:/a.txt");//c:\a.txt (系统找不到指定的文件。)
}
}
详解try{}catch(){}
try{ 可能出现的异常,}catch(异常类型 变量){ 处理异常的代码}catch(异常类型 变量){ 处理异常的代码}
catch 语句块可以多个 ,catch语句块必须从上到下,从小到大捕捉。
try catch 中最多执行一个catch语句块。执行结束后,就结束了。
public class Demo03 {
public static void main(String[] args) {
FileInputStream file;
try {
//JVM创建一个FileNotFoundException类型的对象,并且将该对象的内存地址直接赋值给catch中的e变量
file = new FileInputStream("c:/ajj");
System.out.println("#######");//try中捕捉到错误后下面的代码不再执行,直接catch
file.available();
} catch (FileNotFoundException e) {
//FileNotFoundException将Object中的tostring方法重写
System.out.println(e);//java.io.FileNotFoundException: c:\ajj (系统找不到指定的文件。)
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
关于getMessage 和printStackTrace方法的应用
getMessage取得异常的描述信息
printStackTrace获取异常的堆栈信息(适用于程序调试阶段)
public class Demo04 {
public static void main(String[] args) {
try {
FileInputStream file = new FileInputStream("c:/wsdx");
//Jvm执行了以下demo
//FileNotFoundException e = new FileNotFoundException("c:\\wsdx (系统找不到指定的文件。)");
} catch (FileNotFoundException e) {
e.printStackTrace();//打印详细的信息
String meg = e.getMessage();
System.out.println(meg);//c:\wsdx (系统找不到指定的文件。)
}
System.out.println ("执行");
}
}
finally详解
finally可以直接和try连用
在finally语句块中代码一定执行;只有推出了JVM (System.exit(0);)finally语句块不会执行
通常在程序中为保证某资源一定会释放,一般在finally中 释放资源
finally final finalize 区别
finaliza Object一个方法名,垃圾回收机制,在回收java对象以前,会自动调用finaliz方法
自定义异常:
1.编译时异常Exception
public class CustomerService {
public void register(String name) throws IllegalNameException{
if(name.length()<6) {
//创建异常对象
//IllegalNameException e = new IllegalNameException("用户长度不能小于6位");
//手动抛出异常
//throw e;
throw new IllegalNameException("用户名长度不能小于6位");
}
System.out.println("注册成功");
}
}
public class IllegalNameException extends Exception{
//定义异常一般有两个构造方法
public IllegalNameException() {
}
public IllegalNameException(String msg) {
super(msg);
}
}
2.运行时异常RuntimeException
方法的覆盖与异常
子类永远无法抛出比父类更宽泛的异常