1、概念
异常是一个程序执行期间发生的事件,它中断了正在执行的程序的正常指令流。
2、常见异常
ArrayIndexOutOfBoundsException 数组下标越界异常
NullPointerException 空指针异常
ClassCastException 类型转换异常
StringIndexOutOfBoundsException 字符串索引超出范围抛出的异常
3、捕捉异常
异常捕捉结构由try、catch和finally组成。
- try语句块存放的是可能发生异常的java语句
- catch程序块在try之后,用来激发被捕获的异常
- finally语句块是异常处理结构的最后执行部分,无论try中的代码如何退出,都将执行finally语句块。
4、方法中抛出异常
若某个方法可能会发生异常,但不想在当前方法中处理这个异常,则可以使用throws、throw关键字在方法中抛出异常。
1.使用throws关键字抛出异常
throws关键字通常被应用在声明方法时,用来指定方法可以抛出的异常。多个异常可使用逗号分隔。
public class Shoot {
static void pop() throws NegativeArraySizeException{
//定义方法并抛出NegativeArraySizeException异常
int[] arr = new int[-3]; //创建数组
}
public static void main(String[] args) {
try { //try语句处理异常信息
pop(); //调用pop()方法
} catch (NegativeArraySizeException e) {
System.out.println("pop()方法抛出的异常"); //输出异常信息
}
}
}
注意:throws关键字将异常抛给上一级后,如果不想处理该异常,可以继续向上抛出,但最终要有能够处理该异常的代码。
2.使用throw关键字抛出异常
//创建自定义异常类
public class MyException extends Exception{
String message;
public MyException (String ErrorMessagr) {//父类方法
message = ErrorMessagr;
}
public String getMessage() {//覆盖getMessage方法
return message;
}
}
//使用throw关键字捕捉异常
public class Captor {
static int quotient(int x,int y) throws MyException{//定义方法抛出异常
if (y<0) {
throw new MyException("除数不能是负数");//异常信息
}
return x/y;
}
public static void main(String[] args) {
try { //try语句包含可能发生异常的语句
int result = quotient(3, -1); //调用quotient方法
} catch (MyException e) { //处理自定义异常
System.out.println(e.getMessage());
} catch (ArithmeticException e) { //处理ArithmeticException异常
System.out.println("除数不能为0");
} catch (Exception e) { //处理其他异常
System.out.println("程序发生了其他的异常");
}
}
}
5、异常链
简单实例:
//CrashCar1Exception异常
public class CrashCar1Exception extends Exception{
public CrashCar1Exception() {
super();
// TODO Auto-generated constructor stub
}
public CrashCar1Exception(String message, Throwable cause) {
super(message, cause);
// TODO Auto-generated constructor stub
}
public CrashCar1Exception(String message) {
super(message);
// TODO Auto-generated constructor stub
}
public CrashCar1Exception(Throwable cause) {
super(cause);
// TODO Auto-generated constructor stub
}
}
// CrashCar2Exception异常
public class CrashCar2Exception extends Exception{
public CrashCar2Exception() {
super();
// TODO Auto-generated constructor stub
}
public CrashCar2Exception(String message, Throwable cause) {
super(message, cause);
// TODO Auto-generated constructor stub
}
public CrashCar2Exception(String message) {
super(message);
// TODO Auto-generated constructor stub
}
public CrashCar2Exception(Throwable cause) {
super(cause);
// TODO Auto-generated constructor stub
}
}
// CrashCar3Exception 异常
public class CrashCar3Exception extends Exception{
public CrashCar3Exception() {
super();
// TODO Auto-generated constructor stub
}
public CrashCar3Exception(String message, Throwable cause) {
super(message, cause);
// TODO Auto-generated constructor stub
}
public CrashCar3Exception(String message) {
super(message);
// TODO Auto-generated constructor stub
}
public CrashCar3Exception(Throwable cause) {
super(cause);
// TODO Auto-generated constructor stub
}
}
//
public class CrashCar4Exception extends Exception{
public CrashCar4Exception() {
super();
// TODO Auto-generated constructor stub
}
public CrashCar4Exception(String message, Throwable cause) {
super(message, cause);
// TODO Auto-generated constructor stub
}
public CrashCar4Exception(String message) {
super(message);
// TODO Auto-generated constructor stub
}
public CrashCar4Exception(Throwable cause) {
super(cause);
// TODO Auto-generated constructor stub
}
}
//User 类
public class User {
boolean accelerator ;
String userName;
public boolean isAccelerator() {
return accelerator;
}
public void setAccelerator(boolean accelerator) {
this.accelerator = accelerator;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
}
//测试类
public class Text {
public static void main(String[] args) {
User user =new User();
user.userName =null;
user.accelerator =false;
try {
car4(user);
} catch (CrashCar4Exception e) {
e.printStackTrace();
}
}
public static boolean car1(User user) throws CrashCar1Exception {
if (user.getUserName()!=null) {
if (user.accelerator) {
throw new CrashCar1Exception("第一辆车追尾了");
}else {
return true;
}
}
throw new CrashCar1Exception("车里没人撞车了!");
}
public static boolean car2(User user) throws CrashCar2Exception {
try {
boolean car1 = car1(user);
return car1;
} catch (CrashCar1Exception e) {
e.printStackTrace();
throw new CrashCar2Exception("第二辆车追尾了",e);
}
}
public static boolean car3(User user) throws CrashCar3Exception {
try {
boolean car2 = car2(user);
return car2;
} catch (CrashCar2Exception e) {
e.printStackTrace();
throw new CrashCar3Exception("第三辆车追尾了",e);
}
}
public static boolean car4(User user) throws CrashCar4Exception {
try {
boolean car3 = car3(user);
return car3;
} catch (CrashCar3Exception e) {
e.printStackTrace();
throw new CrashCar4Exception("第四辆车追尾了",e);
}
}
}