知识概要
1.认识异常
- 异常大体上,异常分为编译期异常、运行期异常、逻辑错误;编译期异常在编写代码时会有提示,大多数时表现为语法错误;
(缺少 ; )
运行期异常是在编译无异常后,运行时产生的错误,例如数组越界,除数为0等;
逻辑错误是程序员在代码的理解上产生的错误,表现为运行结果超出预料,可通过调试解决;
异常层次结构图:
- 异常格式:
try{
异常语句
}catch(exceprion e){
}finally{
一定会执行的代码
}
衍生出的其他格式:
try···catch···
try···catch···catch···
try···catch···catch···finally
try···finally
2.处理异常
try { }catch{ }:在catch{}中自定义处理方法,保证程序正常运行到结束;
try{
int i=2/0;
}catch{
System.out.println("除数不能为0!");
}
对应try catch的多种写法:
try{ } catch(Exception e1){ } catch(Exception e2){ }.....
catch中的异常类别自上往下越来越大(对应各种错误的分别处理)
try{ }catch(异常1|异常2|异常3.....){ }
catch中异常只能是同级别,不能有子父级关系
demo1: 在线测试
package cn.tedu.exce;
/*
* 练习
* 若程序正常执行,finally代码块中的代码一定会正常执行
* System.exit(0);退出虚拟机
* 对应try catch的多种写法
* try{} catch(){} catch(){}.....
* catch中的异常类别自上往下越来越大(对应各种错误的分别处理)
* try{}catch(异常1|异常2|异常3.....){}
* catch中异常只能是同级别,不能有子父级关系
*
* */
public class Demo3 {
public static void main(String[] args)
{
System.out.println(method());
}
public static int method()
{
int a=10;
try{
a=20;
a=a/0;
}
catch (Exception e)
{
a=30;
System.out.println(a);
//System.exit(0);
//在finally块之前有return的话
//finally在return中间执行
return a;//return 30;
}
finally{
a=40;
System.out.println(a);
}
return a;
}
}
demo2: 在线测试
package cn.tedu.exce;
public class Demo7 {
public static void main(String[] args) throws Exception {
try {
try {
System.out.println("这里是内部try");
} catch (arithmeticException e1) {
System.out.println("这里是内部catch");
}
} catch (Exception e2) {
System.out.println("这里是外部catch");
}
System.out.println("程序结束");
}
}
3.常见异常
- 常见异常:
异常 | 描述 | 举例 |
---|---|---|
ArithmeticException(算数异常) | 除数为0,则发生该异常 | int i=1/0; |
NullPointerException(空指针异常) | 如果一个对象没有实例化,那么访问该对象或调用它的方法就会发生NullPointerException异常 | Image im[]=new Image[4]; System.out.println(im[0].toString()); |
NegativeArraySizeException | 数组元素个数为负时,会发生该异常 | int[] arr=new int[3]; int i=arr[-1]; |
ArrayIndexOutOfBoundsExceptionJava(数组越界异常) | 数组下标越界,会发生ArrayIndexOutOfBoundsExceptionJava异常 | int[] arr=new int[3]; for(int j=0;j<5;j++){ int b=arr[i] ;} |
ArrayStoreException | 存取数组中错误的数据类型 | |
FileNotFoundException | 存取一个不存在的文件 | |
IOException | 通常的I/O错误 |
还有其他异常,就不一一列出;
4.关键字throws与throw及其区别
- throws:方法定义的时候在方法后面,在方法后面提示调用者,即抛给调用者处理,可以 一直往上throws,即交给虚拟机处理;
public static void main(String args[]) throws Exception{
System.out.println(Num());
}
public static int Num() throws ArithmeticException{
return 2/0;
}
- throw:抛出异常,抛出的时候直接抛出异常类的实例化对象,它的使用一般配合try catch、finally、throws使用;
public static void main(String[] args) throws Exception {
try {
throw new Exception("实例化对象");
} catch (Exception e) {
System.out.println(e);
}
}
5.自定义异常
- 自定义异常处理类:自定义的异常类直接继承Excetion,就可以完成自定义异常类;
class MyException extends Exception{
public MyException(){
super("自定义的异常类");
}
}
- 自定义异常的使用:
小demo: 在线测试
package cn.tedu.exce;
import java.util.Scanner;
/**
* 练习:自定义一个异常,对从控制台接收的分数判断,不能小于0,大于100,否则抛出异常
*/
public class Demo6 {
public static void main (String[] args) throws mysException
{
try{
System.out.println("请输入一个分数在0-100之间");
Scanner sc=new Scanner(System.in);
Integer num=Integer.parseInt(sc.next());
if(num>=0 &&num<=100)
{
System.out.println("成绩正确");
}
else
{
System.out.println("成绩输入数字有误!");
}
}
catch(Exception e)
{
throw new mysException("成绩输入格式有误!");
}
}
}
class mysException extends Exception{
public mysException(String msg)
{
super(msg);
}
}