检查异常:主动处理
- 捕获: 使用 try catch 块处理
- 抛出: 在方法上声明可能发生的异常,由调用者来处理
常见异常:
try …catch…
public class Test {
public static void main(String[] args) {
try {
System.out.println(1 / 0);
} catch (Exception e) {
// 打印异常跟踪的信息
e.printStackTrace();
System.out.println("分母为零");
}
finally{
System.out.println("end...");
}
}
}
结果:
java.lang.ArithmeticException: / by zero
at com.cao.test.lesson04.Test.main(Test.java:6)
分母为零
end...
也可以根据异常的类型进入不同的 catch 块
public class Test {
public static void main(String[] args) {
try {
System.out.println(1 / 1);
int[] arr=new int[2];
System.out.println(arr[2]);
} catch (ArithmeticException a) {
// 打印异常跟踪的信息
a.printStackTrace();
System.out.println("分母为零");
}catch (ArrayIndexOutOfBoundsException A){
A.printStackTrace();
System.out.println("数组越界...");
}
finally{
System.out.println("end...");
}
}
}
执行结果:
1
数组越界...
end...
java.lang.ArrayIndexOutOfBoundsException: 2
at com.cao.test.lesson04.Test.main(Test.java:8)
有时候也会用异常来进行分支
public class Test1 {
public static void main(String[] args) {
try{
System.out.println(1/1);
System.out.println(args[0]);
}catch (ArithmeticException A){
System.out.println("-------");
}catch (ArrayIndexOutOfBoundsException AA){
System.out.println("+++++++");
}
}
}
执行结果:
1
+++++++
throws语句:
public class Test {
public void loadClass() throws ArithmeticException {
System.out.println(1/0);
}
}
public class Start {
public static void main(String[] args) {
Test test = new Test();
try {
test.loadClass();
} catch (ArithmeticException e) {
System.out.println("error...");
e.printStackTrace();
}
}
}
error...
java.lang.ArithmeticException: / by zero
at com.cao.test.lesson04.Test.loadClass(Test.java:6)
at com.cao.test.lesson04.Start.main(Start.java:9)
一个需要注意的地方
public class Test2 {
int i=1;
public int show(){
try {
System.out.println(1 / 0);
System.out.println("......");
return ++i;
} catch (ArithmeticException A){
System.out.println("捕获异常...");
return ++i;
}finally {
System.out.println("end...");
return ++i;
}
}
public static void main(String[] args) {
Test2 test2=new Test2();
int res=test2.show();
System.out.println(res);
System.out.println(test2.i);
}
}
结果:
捕获异常...
end...
3
3
在catch (ArithmeticException A){ System.out.println("捕获异常..."); return ++i;}
的时候,执行了++i
但是并没有return
。
自定义异常:
1.内置异常不可能始终足以捕获所有错误,因此需要用户自定义的异常类;
2.用户自定义的异常类应为 Exception 类(或者 Exception 类的子类)的子类;
3.创建的任何用户自定义的异常类都可以获得 Throwable 类定义的方法 ;
CustomException:
public class CustomException extends RuntimeException{
public CustomException(){
}
public CustomException(String message){
super(message);
}
}
User:
public class User {
public void login(String userName){
if(userName=="admin"){
throw new CustomException("用户名不能为admin...");
}
else{
System.out.println("登陆成功...");
}
}
public static void main(String[] args) {
User user=new User();
user.login("admin");
}
}
结果:
Exception in thread "main" com.cao.test.lesson04.demo.CustomException: 用户名不能为admin...
at com.cao.test.lesson04.demo.User.login(User.java:6)
at com.cao.test.lesson04.demo.User.main(User.java:16)
若User为:
public class User {
public void login(String userName) {
if (userName == "admin") {
throw new CustomException("用户名不能为admin...");
} else {
System.out.println("登陆成功...");
}
}
public static void main(String[] args) {
User user = new User();
try {
user.login("admin");
} catch (CustomException c) {
System.out.println("用户名不能为admin...");
}
}
}
结果为:
用户名不能为admin...
throw 和 throws 的区别:
1)throw 是语句抛出一个异常;throws 是方法抛出一个异常; throws 可以单独使用,但throw 不能;
2)throws 出现在方法函数头;而 throw 出现在函数体;
3)throws 表示出现异常的一种可能性,并不一定会发生这些异常;throw 则是抛出了异常,执行 throw 则一定抛出了某种异常;
4)两者都是消极处理异常的方式(这里的消极并不是说这种方式丌好),只是抛出或者可能抛出异常,但是不会由函数去处理异常,真正的处理异常由函数的上层调用处理。