# 异常机制

# 什么是异常机制

异常机制是指当程序出现错误后,程序如何处理。它提供了程序退出的安全通道。

# 程序错误类型

  • 编译错误: 不遵循语法规则,会抛出该错误
  • 运行时错误: 程序运行时,运行环境发现了不能执行的操作
  • 逻辑错误: 程序没有按照预期的逻辑顺序执行.

# 异常和错误的区别

  • 异常 能够被程序本身处理。并且程序中应当尽可能去处理这些异常

  • 错误 无法处理,程序崩溃。因为它们在应用程序的控制和处理能力之外,而且绝大多数是程序运行时不允许出现的状况

  • 实践 exception架构

# 异常处理方法

  • 抛出异常
  • 捕获异常

# 抛出异常的方式

  • throw: 语句抛出异常
  • throws: 方法抛出异常

# 异常机制抽象结构

exception架构

# Throwable抽象结构

public class Throwable implements Serializable {

    private static final StackTraceElement[] UNASSIGNED_STACK = new StackTraceElement[0];
    
    private String detailMessage;

    private Throwable cause = this;

    private StackTraceElement[] stackTrace = UNASSIGNED_STACK;

    /**
         * Native code saves some indication of the stack backtrace in this slot.
    */
    private transient Object backtrace;


    public Throwable(String message) {
            fillInStackTrace();
            detailMessage = message;
        }

    public Throwable() {
            fillInStackTrace();
        }

    public synchronized Throwable fillInStackTrace() {
            if (stackTrace != null ||
                backtrace != null /* Out of protocol state */ ) {
                fillInStackTrace(0);
                stackTrace = UNASSIGNED_STACK;
            }
            return this;
        }

    private native Throwable fillInStackTrace(int dummy);
}

# 错误

# Error的抽象结构

  • 源码
public class Error extends Throwable {
    static final long serialVersionUID = 4980196508277280342L;

    public Error() {
            super();
    }

    public Error(String message) {
            super(message);
        }

}
  • 使用注意事项 语法规则方面,与 非检查异常使用方式类似,如 throw new Error();,外层不需要进行捕获。

# 常见的Error实现

  • VirtualMachineError
 public abstract class VirtualMachineError extends Error {
    
}
  • OutOfMemoryError
public class OutOfMemoryError extends VirtualMachineError {
    
}
  • ThreadDeath(线程死锁)
public class ThreadDeath extends Error {
}
  • StackOverFlowError
public
class StackOverflowError extends VirtualMachineError {
    
}

# 异常

# 异常抽象结构

  • 源码
public class Exception extends Throwable {
    static final long serialVersionUID = -3387516993124229948L;

    public Exception() {
            super();
    }

    public Exception(String message) {
            super(message);
        }
}
  • 使用注意事项 抛出异常时,语法规则要求必须进行处理。 也就是说,默认情况下,异常是当成非运行时异常使用的exception使用

# 异常的分类

  • 非运行时异常编译异常或者可察异常

RuntimeException以外的异常都是可察异常,类型上都属于Exception类及其子类。

  • 运行时异常或者不可察异常

RuntimeException及其子类 以及 错误

# 常见运行时异常

  • IndexOutOfBoundsException
public
class IndexOutOfBoundsException extends RuntimeException {
    
}

  • IllegalArgumentException
public
class IllegalArgumentException extends RuntimeException {
    
}
  • NullPointerException
public
class NullPointerException extends RuntimeException {
    
}

# 常见非运行时异常(编译异常)

  • IOException
public
class IOException extends Exception {
    
}
  • FileNotFoundException
public class FileNotFoundException extends IOException {
    
}
  • SQLException
public class SQLException extends java.lang.Exception
                          implements Iterable<Throwable> {
    
                          }