api 分成以下几个包讲解:
第二章 异常类1. java.lang (异常类,String,Math,StringBuffer,StringBuilder,包装类,Object,Class)
2. java.Util (日期类 工具类 集合类)
3. java.io (文件类 文件读写类...)
4. java.net (了解)
5. 多线程 (lang 重点)
6. java.awt(了解)
7. jdk8+ api (了解API的特性 Stream流)
什么是异常?
程序编译或运行当中,出现的问题,系统统称为异常。异常分为:程序异常Exception和错误Error。
异常根类:Throwable
程序异常类:Exception 由于程序员疏忽,设计不合理导致的问题,需要解决
程序错误类:Erro 程序中出现致命问题 如虚拟机崩溃,内存溢出,堆栈溢出...不需要在程序中解决
Exception的分类
1.编译时异常:在程序编译时发生了异常,如果不解决,无法编译
例如:IOException (IO 异常) SQLException(数据库异常) FileNotFoundException(文件未找到异常) ....
2. 运行时异常:编译时没有问题,运行时抛出异常(RuntimeException )
例如:
| 异常类 | 含义 | 示例 |
| ArrayIndexOutofBoundsException | 数组下标越界异常 | int[] a = new int[4]; System.out.println(a[4]); |
| NullPointerException | 空引用异常 | String str = null; System.out.println(str.length()); |
| ArithmeticException | 数学异常 | 10 / 0 |
| ClassCastException | 类型转换异常 | Son s = (Son)new Father(); |
| NumberFormatException | 数字转换异常 | Integer.parseInt("ys") |
public class TestException1 {
public static void main(String[] args) {
//数学异常
//System.out.println(10 / 0);
//数组越界
// int[] a = new int[4];
// System.out.println(a[4]);
//空引用
// String str = null;
// System.out.println(str.length());
//类型转换
// Son s = (Son)new Father();
//数字转换
System.out.println(Integer.parseInt("ys") + 10);
}
}
出现异常后的反应
总结:异常在程序中必须解决1.系统打印错误信息
2.程序终止,突然中断
3.分配对象信息资源不变,可能资源泄露
异常捕获机制
异常的处理: 异常的捕获机制 (try catch finally)
语法规则:
//监视器
try{
//代码监视器,有可能发生异常的代码块
}catch(异常类型 变量名){
//异常捕获器,处理问题
}catch(异常类型 变量名){
//异常捕获器
} //...
finally{
//清理块,无条件执行的语句块
}
注意:
1.try 监视器,不可以单独出现,需要配合catch(可以处理异常)或finally(不会处理异常)
2.catch可以有,捕获不同类型的异常,类似多条分支结果,n选1个,类型不要重复
3.因此将catch(Exception e) {} 放到catch的最后
4.finally 可以写也可以不写,无条件执行,除非退出虚拟机,包括return
public class SystemCode {
public static void main(String[] args) {
System.out.println("以下张雨代码....");
try {
System.out.println("..........");
System.out.println(10 / 0);
}
catch(ArithmeticException e) {
System.out.println("你发生了数学错误,赶紧改正");
//return;
//退出虚拟机
System.exit(0);
}
catch(Exception e) {
//保底
}
finally {
System.out.println("无条件执行");
}
System.out.println("张雨代码结束....");
System.out.println("以下义民代码....");
System.out.println("..........");
System.out.println("义民代码结束....");
}
}
运行顺序:
情况1:
try {
System.out.println(10 / 0);
}
catch (Exception e) {
System.out.println("数学异常");
}
finally {
System.out.println("执行finally");
}
System.out.println("执行该语句");
情况2:
try {
System.out.println(10 / 10);
}
catch (Exception e) {
System.out.println("数学异常");
}
finally {
System.out.println("执行finally");
}
System.out.println("执行该语句");
情况3:
try {
System.out.println(10 / 0);
}
finally {
System.out.println("执行finally");
}
System.out.println("执行该语句");
情况4:
try {
System.out.println(10 / 0);
}
catch (Exception e) {
System.out.println("数学异常");
return;
}
finally {
System.out.println("执行finally");
}
System.out.println("执行该语句");
注意: 先执行finally ,再执行return
情况5:
try {
System.out.println(10 / 0);
}
catch (Exception e) {
System.out.println("数学异常");
System.exit(0);
}
finally {
System.out.println("执行finally");
}
System.out.println("执行该语句");
异常的抛出
throws
当方法内有异常发生,不解决,难以解决时,将方法内的异常向外抛出,使用throws。
语法:
控制权限 返回值 方法名(参数类型 参数变量) throws 异常类型1, 异常类型2... {
//有异常
}
注意:
1. 方法的异常抛给方法的调用者,调用者必须解决或继续抛,直到主方法,主方法抛给虚拟机
public class TestThrows {
//方法向外抛出异常
public static void test1() throws NullPointerException,ArithmeticException{
System.out.println(10 / 0);
}
//调用后不解决,继续抛
public static void test2() throws Exception{
test1();
}
public static void main(String[] args) {
try {
//调用者
test1();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
2. 方法重写时,子类不允许抛出更大类型异常
public class A {
public void test() throws NullPointerException{
}
}
class B extends A{
//重写不能抛出更大类型
// public void test() throws Exception{
//
// }
//重载不限制
public void test(int a) throws Exception{
}
}
3. 一般都是编译时异常抛出
4. throws 抛出异常类
thorw
当程序员需要手动创造一个异常,并且抛出去。自己构建一个异常。
语法:
//1 创建一个异常对象
Exception 对象名 = new Exception("异常的问题");
//2 throw抛出
throw 对象名;
例子:
public class TestThrow {
public static void main(String[] args) {
//使用检测年龄方法
checkAge(9);
System.out.println("继续编码代码");
}
public static void checkAge(int age) {
if(age > 0 && age < 150) {
System.out.println("年龄合法");
}else {
//构建异常
RuntimeException e = new RuntimeException("年龄不合法,不能超出(1-150)");
//手动抛出异常
throw e;
}
}
}
throws 和 throw 区别?
| throws | throw |
| 方法向外自动抛出异常 | 手动创建异常抛出 |
| 异常类 | 异常对象 |
| 方法后面 | 方法里 |
自定义异常
程序员自己构建一个异常类型,需要继承异常父类: RuntimeException或者Exception。
public class AgeException extends Exception{
private static final long serialVersionUID = 1L;
public AgeException() {
super();
// TODO Auto-generated constructor stub
}
public AgeException(String message) {
super(message);
// TODO Auto-generated constructor stub
}
}
public static void checkAge(int age) throws AgeException {
if(age > 0 && age < 150) {
System.out.println("年龄合法");
}else {
throw new AgeException("年龄非法");
}
}



