异常就是指程序运行时出现错误时通知调用者的一种机制。(已经通过编译得到class文件了,交由JVM执行时出现的错误!)
1.异常的基本用法
1.1捕获异常
基本用法:
try{
有可能出现异常的语句 ;
}[catch (异常类型 异常对象) {
} … ]
[finally {
异常的出口
}]
注意:
try 代码块中放的是可能出现异常的代码.
catch 代码块中放的是出现异常后的处理行为.
finally 代码块中的代码用于处理善后工作, 会在最后执行.
其中 catch 和 finally 都可以根据情况选择加或者不加.
代码示例:
1.首先来看一下不做处理的异常:
public class TestException {
public static void main(String[] args) {
int[] arr={1,2,4};
System.out.println(arr[100]);
System.out.println(arr[1]);
}
}
运行结果为:
从运行结果来看,如果不处理异常的话,程序的异常就会交由JVM去处理,此时程序就会强制结束!
2.使用try...catch来处理异常:
public class TestException {
public static void main(String[] args) {
int[] arr={1,2,4};
try{
System.out.println(arr[100]);
}catch (ArrayIndexOutOfBoundsException e){
e.printStackTrace(); //打印调用栈的异常信息!
System.out.println("下标越界异常!");
}
System.out.println(arr[1]);
}
}
运行结果为:
从结果可以知道,通过try…catch捕获异常,程序的异常就会按照我们的设想进行,不会再交由JVM去处理,此时的arr[1]也打印出来了。
3.使用多个catch的情况:
public class TestException {
public static void main(String[] args) {
int[] arr={1,2,4};
try{
System.out.println(arr[100]);
}catch ( NullPointerException e){
e.printStackTrace(); //打印调用栈的异常信息!
System.out.println("空指针异常!");
}catch (ArrayIndexOutOfBoundsException e){
e.printStackTrace();
System.out.println("下标越界异常!");
}
System.out.println(arr[1]);
}
}
运行结果为:
使用多个catch来捕获异常信息,其实和if…else的用法类似,如果多个catch的异常类型不匹配的话,同样也是捕获不了的,最后也是会交由JVM来做处理!
4.结合finaly来使用,在有些使用场景,如释放资源等,要搭配finaly来使用:
import java.util.InputMismatchException;
import java.util.Scanner;
public class TestException {
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
try {
while (scanner.hasNext()) {
int result = scanner.nextInt();
System.out.println(result);
}
}catch (InputMismatchException e){
e.printStackTrace();
}
finally {
scanner.close();
System.out.println("关闭资源!");
}
}
}
运行结果为:
使用了finally,无论程序是否有异常,finally里内的语句都会执行,从而保障了scanner的close方法的执行;从而释放了资源,做好了善后工作!
5.如果本方法里没有合适的处理异常的方式,就会沿着调用栈向上传递:
public class TestException {
public static void main(String[] args) {
int[] arr={1,2,3};
try {
print(arr);
}catch (ArrayIndexOutOfBoundsException e){
e.printStackTrace();
System.out.println("下标越界异常!");
}
}
private static void print(int[] arr) {
System.out.println(arr[100]);
}
}
运行结果:
在print方法里没有处理异常的方式,就沿着调用栈从而找到了main方法里,此时mian方法里有处理异常的方式,就不会再向上传递了!
异常处理流程
程序先执行 try 中的代码
如果 try 中的代码出现异常, 就会结束 try 中的代码, 看和 catch 中的异常类型是否匹配.
如果找到匹配的异常类型, 就会执行 catch 中的代码
如果没有找到匹配的异常类型, 就会将异常向上传递到上层调用者.
无论是否找到匹配的异常类型, finally 中的代码都会被执行到(在该方法结束之前执行).
如果上层调用者也没有处理的了异常, 就继续向上传递.
一直到 main 方法也没有合适的代码处理异常, 就会交给 JVM 来进行处理, 此时程序就会异常终止
1.2抛出异常
除了 Java 内置的类会抛出一些异常之外, 程序猿也可以手动抛出某个异常. 使用 throw 关键字完成这个操作。
示例代码:
public class TestException {
public static void main(String[] args) {
try {
System.out.println(divide(10, 0));
}catch (ArithmeticException e){
e.printStackTrace();//打印异常信息!!
}
System.out.println("后面的代码!");
}
public static int divide(int x, int y) {
if (y == 0) {
throw new ArithmeticException("算术异常!!!");
}
return x / y;
}
}
运行结果为:
通过throw这个关键字,我们可以手动抛出异常,这个异常可以是Java本来就定义好的,也可以是我们自己定义的异常类;可以通过try…catch来捕获我们抛出的异常,这样后续代码的执行就不受影响,如果不捕获那就会交由JVM来处理,程序也就终止了!
注意(throws):
throws关键字的意思就是可能会抛出异常的声明!!和throw的作用是不同的;要注意区别看待!
示例代码:
public class TestException {
public static void main(String[] args) {
try {
System.out.println(divide(10, 0));
}catch (ArithmeticException e){
e.printStackTrace();//打印异常信息!!
}
System.out.println("后面的代码!");
}
public static int divide(int x, int y) throws ArithmeticException{
if (y == 0) {
throw new ArithmeticException("算术异常!!!");
}
return x / y;
}
}
运行结果:
我们可以使用 throws 关键字, 把可能抛出的异常显式的标注在方法定义的位置. 从而提醒调用者要注意捕获这些异常。
3.异常体系
顶层类Thorwable派生出两个重要的子类, Error 和 Exception;其中 Error 指的是 Java 运行时内部错误和资源耗尽错误. **应用程序不抛出此类异常**. 这种内部错误一旦出现,除了告知用户并使程序终止之外, 再无能无力. 这种情况很少出现。Exception 是我们程序猿所使用的异常类的父类.其中 Exception 有一个子类称为 RuntimeException , 这里面又派生出很多我们常见的异常类NullPointerException , IndexOutOfBoundsException 等
Java语言规范将派生于 Error 类或 RuntimeException 类的所有异常称为 非受查异常, 所有的其他异常称为 受查异常。
如果一段代码可能抛出 受查异常, 那么必须显式进行处理,显式处理有两种方法:a.使用try…catch包裹或b.使用异常声明(throws)交给上级调用者处理。
a.示例(使用try…catch处理):
import java.util.Scanner;
class Myexception extends Exception{ //自定义一个异常类
public Myexception(String message) {
super(message);
}
}
public class TestMyexception {
/**
* 实现一个简单的控制台版用户登陆程序, 程序启动提示用户输入用户名密码.
* 如果用户名密码出错, 使用自定义异常的方式来处理
*/
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String pwd = scanner.next();
Mytest(pwd);
}
public static void Mytest(String pwd) {
if ("123456".equals(pwd) == false) {
try {
throw new Myexception("密码错误!");//抛出一个自定义异常
} catch (Myexception e) {
e.printStackTrace();
}
} else {
System.out.println("登录成功!");
}
}
}
运行结果:
b.示例(使用异常声明throws处理):
import java.util.Scanner;
class Myexception extends Exception{ //自定义一个异常类
public Myexception(String message) {
super(message);
}
}
public class TestMyexception {
/**
* 实现一个简单的控制台版用户登陆程序, 程序启动提示用户输入用户名密码.
* 如果用户名密码出错, 使用自定义异常的方式来处理
*/
public static void main(String[] args) throws Myexception {
Scanner scanner = new Scanner(System.in);
String pwd = scanner.next();
Mytest(pwd);
}
public static void Mytest(String pwd) throws Myexception {
if ("123456".equals(pwd) == false) {
throw new Myexception("密码错误!");//抛出一个自定义异常
} else {
System.out.println("登录成功!");
}
}
}
运行结果:
4.自定义异常
Java 中虽然已经内置了丰富的异常类, 但是我们实际场景中可能还有一些情况需要我们对异常类进行扩
展, 创建符合我们实际情况的异常。
例如, 我们实现一个用户登陆功能:
public class Test {
private static String userName = "admin";
private static String password = "123456";
public static void main(String[] args) {
login("admin", "123456");
}
public static void login(String userName, String password) {
if (!Test.userName.equals(userName)) {
// TODO 处理用户名错误
}
if (!Test.password.equals(password)) {
// TODO 处理密码错误
}
System.out.println("登陆成功");
}
}
此时用户名密码错误就可能要抛出两种异常,我们就可以基于已有的异常类来进行拓展,创建和我们需求相关的异常类。
class UserError extends Exception {
public UserError(String message) {
super(message);
}
}
class PasswordError extends Exception {
public PasswordError(String message) {
super(message);
}
}
此时的login代码就可以修改成:
public static void main(String[] args) {
try {
login("admin", "123456");
} catch (UserError userError) {
userError.printStackTrace();
} catch (PasswordError passwordError) {
passwordError.printStackTrace();
}
}
public static void login(String userName, String password) throws UserError,PasswordError {
if (!Test.userName.equals(userName)) {
throw new UserError("用户名错误");
}
if (!Test.password.equals(password)) {
throw new PasswordError("密码错误");
}
System.out.println("登陆成功");
}
5.总结
异常就是指程序运行时出现错误时通知调用者的一种机制;
自定义异常通常会继承自Exception或者 RuntimeException;
继承自Exception默认是受查异常需要显式处理,继承自 RuntimeException默认是非受查异常不需要显式处理。