Java异常和异常处理
1. 错误和异常
- 错误–JVM无法解决
// java不正常情况称为异常
// 异常和错误:
// — Error 和Expection 都继承 Throwable
// — Error分类
// — VirtualMachineError 虚拟栈错误
// — StackOverflowError 栈内存溢出
// — OutOfMemoryError 内存溢出
// — NoClassDefFoundError 无法找到某个类定义错误
// — UnsportedClassVersionError 不支持该字节码版本错误,一般运行JDK版本与编译不同导致
- 异常 使用针对性代码处理
常见异常子类:
- NullPointExpection 空指针异常
- FileNotFoundExpection 文件找不到异常
- IOExpection 数据读写异常
- ClassCastExpection 类转换异常
- SQLExpection 数据库SQL语句执行异常
2. 异常信息查看
- 异常类型。
- 相关描述信息,有的异常对象没有描述信息。
- 异常的堆栈跟踪信息,即异常在哪里抛出,经历过那些方法。
public static void main(String[] args) {
Object o = new Object();
String str= (String) o;
}
Exception in thread "main" java.lang.ClassCastException: // 线程和异常类型
java.lang.Object cannot be cast to java.lang.String// 描述信息
at indi.Jihad.a1.TestException.main(TestException.java:15)// 异常跟踪
异常的意义:
- 开发人员便于发现发生了什么错误。
- 从堆栈跟踪获取代码错误所在位置,开发人员给出合适的处理。
3. Throwable 类
构造器:
- Throwable() 无信息的异常对象。
- Throwable(String message) 有信息的异常对象。
- Throwable(Throwable case) 包裹异常的异常,该异常示意由case引起。
- Throwable(String message,Throwable case) 有信息的、包裹异常的异常。
- Throwable getCase() 获取引起异常的原因。
- String message() 获取异常信息。
- void printStackTrace()标准错误方式打印异常类型、描述信息、堆栈跟踪信息。
4. 异常处理
4. 1 try-catch-finally
// 格式
try{
// 有可能出现异常的代码块
}catch(Expection1 e){ // 捕获特定异常1
// 异常1处理
}catch(Expection1 e){ // 捕获特定异常2
// 异常2处理
}.....
注意:上面的Expection类型 <= 下面的Expection类型
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (true){
int i = scanner.nextInt();
try {
if (i==1){
throw new NullPointerException("catch NullPointerException");
} else if (i == 2) {
throw new ClassCastException("catch ClassCastException");
}else if (i==3){
throw new ArrayIndexOutOfBoundsException("catch ArrayIndexOutOfBoundsException");
}else {
throw new RuntimeException("catch RuntimeException");
}
}catch (NullPointerException e){
System.out.println(e.getMessage());
}catch (ClassCastException e){
System.out.println(e.getMessage());
}catch (ArrayIndexOutOfBoundsException e){
System.out.println(e.getMessage());
}catch (RuntimeException e){
System.out.println(e.getMessage());
}
}
}
- finally 回调代码
public class TestExpection1 {
public static void main(String[] args) {
System.out.println(getResult());//2
}
public static int getResult(){
try {
if (true)throw new Exception();
return 1;
}catch (Exception e){
return 3;
}
finally {// 在return 前执行
return 2;
}
}
}
finally 会在return 之前必然执行。所以在finally写的代码都会是实现(非异常出现)。
4. 2 throws
throws 作为抛出异常由上一级调用的方法处理,可以抛出多个异常。
public static void main(String[] args) {
try {
System.out.println(getResult());//2
}catch (RuntimeException e){
System.out.println(2);
}catch (Exception e){
System.out.println(3);
}
}
public static int getResult() throws RuntimeException,Exception {
if (true) throw new Exception();
return 1;
}
如果抛出的异常为运行是异常,编译器不会提醒我们捕获或抛出,这时候我们就得保持警惕,并决定如何处理。
5. JDK7 异常新特性
- catch可以多捕获异常,多种异常使用
|
隔开。 - 多捕获异常,异常有隐式的finall修饰,程序不能更改异常的地址值。
public static void main(String[] args) {
try{
getResult(1);
}catch (NullPointerException | ClassCastException e2){
System.out.println("hello ");
}
}
public static int getResult(int num) {
if (num==1){
throw new NullPointerException();
}else if (num==2){
throw new ClassCastException();
}else throw new RuntimeException();
}
}