- 异常概述与异常体系结构(常见异常)
- 异常中的错误Error
- 异常中的Exception
- 异常处理机制
- 异常的处理:抓抛模型
- 一、tray-catch-finally
- 二、throws方式
- 手动抛出异常
- 自定义异常
- 综合
public class ErrorTest {
public static void main(String[] args) {
// main(args);//递归调用,栈溢出,main方法调用main方法:java.lang.StackOverflowError
Integer[] arr = new Integer[1024*1024*1024];//堆溢出OOM,java.lang.OutOfMemoryError
}
}
异常中的Exception
编译时异常比较严重,编译时就报错了
常见的运行时异常
import java.io.File;
import java.io.FileInputStream;
import java.util.Date;
import java.util.Scanner;
import org.junit.jupiter.api.Test;
public class ExceptionTest {
/
public class FianllyTest {
@Test
public void test1() {
try {
int a = 10;
int b = 0;
System.out.println(a/b);
}catch(ArithmeticException e) {
//e.printStackTrace();
int[] arr = new int[10];
System.out.println(arr[10]);//catch中又有错误,没有try去捕获它
}catch(Exception e2) {
e2.printStackTrace();
}finally {
System.out.println("一定会执行");
}
}
/
public class OverrideTest {
}
class SuperClass{
public void method() throws IOException{
}
}
class SubClass extends SuperClass{
public void method() throws FileNotFoundException{
}
}
手动抛出异常
public class StudentTest {
public static void main(String[] args) {
Student s= new Student();
try {
s.regist(-1001);
} catch (Exception e) {
// e.printStackTrace();
System.out.println(e.getMessage());//Message = "您输入的数据非法!"
}
System.out.println(s);
}
}
class Student {
private int id;
public void regist(int id) throws Exception {
if(id > 0) {
this.id=id;
}else {
// System.out.println("您输入的数据非法!");
//手动抛出一个异常对象
throw new Exception("您输入的数据非法!");//若输入的数据小于0,手动抛出异常。
}
}
@Override
public String toString() {
return "Student [id=" + id + "]";
}
}
自定义异常
//自定义一个异常类MyException
public class MyException extends RuntimeException{
static final long serialVersionUID = -703412310745766939L;//标识该异常的序列号
public MyException() {
}
public MyException(String msg) {
super(msg);
}
}
public class StudentTest {
public static void main(String[] args) {
Student s= new Student();
try {
s.regist(-1001);
System.out.println(s)
} catch (Exception e) {
// e.printStackTrace();
System.out.println(e.getMessage());//Message = "不能输入负数!"
}
}
}
class Student {
private int id;
public void regist(int id) throws Exception {
if(id > 0) {
this.id=id;
}else {
//System.out.println("您输入的数据非法!");
//手动抛出一个异常对象
//throw new Exception("您输入的数据非法!");//若输入的数据小于0,手动抛出异常。
throw new MyException("不能输入负数");//MyException是自定义异常类
}
}
@Override
public String toString() {
return "Student [id=" + id + "]";
}
}
练习:
综合进入方法A
用A方法的finally
制造异常
进入方法B
调用方法B的finally
//自定义异常类
public class EcDef extends Exception{
static final long serialVersionUID = -33875164229948L;
public EcDef() {
}
public EcDef(String msg) {
super(msg);
}
}
public class EcmDef {
public static void main(String[] args) {
try {
int i=Integer.parseInt(args[0]);
int j=Integer.parseInt(args[1]);
int resual = ecm(i,j);
}catch(NumberFormatException e) {
System.out.println("数据类型不一致");
}catch(ArrayIndexOutOfBoundsException e){
System.out.println("缺少命令行参数");
}catch(ArithmeticException e) {
System.out.println("除0错误");
}catch(EcDef e) {
System.out.println(e.getMessage());
}
//获取命令行参数的值,直接运行会报错--->缺少命令行参数
//run Configurations--->自变量
}
public static int ecm(int i, int j) throws EcDef{
if(i<=00 || j<0) {
throw new EcDef("不能为负数");
}
return i/j;
}
}
面试题:
1、final , finally , finalize 的区别?
结构相似类似:
throw 和 throws:
thow 表示抛出一个异常类的对象,生成异常对象的过程,声明在方法体内。
thows 属于异常处理的一种方式,声明在方法的声明处。
Collection 和 Collections
String 、StringBuffer 、 StringBuilder
ArrayList 、linkedList
HashMap、linkedHashMAp
重写、重载
结构不相似:
接口、抽象类
== 、equals();
sleep 、wait()



