异常
Throwable
throwable分为Error和Exception。
Error
Error是程序无法处理的错误,表示运行应用程序中较严重的问题。
- 虚拟机错误
- 内存溢出
- 线程锁死
- ……
Exception
- Unchecked Exception 编译器不要求强制处理的异常。
- RuntimeException
- NullPointerException 空指针异常
- ArrayIndexOutOfBoundsException数组下标越界
- ArithmeticException 算数异常
- ClassCastException 类型转换异常
- RuntimeException
- Checked Exception 在编写程序时,如果出现错误,集成环境就会提示。
- IOException IO异常
- SQLException SQL异常
处理机制
抛出异常
- 异常类型
- 异常出现时的程序状态
捕捉异常
- try 执行可能产生异常的代码
- catch 捕获异常
- finally 无论是否发生异常代码总能执行
声明异常
throws:声明可能要抛出的异常
抛出异常
throw 手动抛出异常
多重catch结构
/**
try后面可以跟多重catch。但是后面的Exception类必须不是前一个Exception类的子类。
*/
try{
...
}catch(ArithmeticException e){
...
}catch(InputMismatchException e){
...
}catch(Exception e){
...
}
return关键字在异常处理中的作用
public int test(){
Scanner input = new Scanner(System.in);
System.out.println("运行开始");
try{
System.out.println("请输入第一个整数:");
int one = input.nextInt();
System.out.println("请输入第二个整数:");
int two = input.nextInt();
return one/two;
}catch(ArithmeticException e){
System.out.println("除数不允许为零");
return 0;
}finally{
System.out.println("=====运算结果=====");
return -1;
}
}
按照逻辑,输入12和4应该直接输出3
/**
请输入第一个整数:12
请输入第二个整数:4
=====运算结果=====
-1
*/
按照逻辑,输入12和0将会抛出ArithmeticException异常,输出0
/**
请输入第一个整数:12
请输入第二个整数:0
=====运算结果=====
-1
*/
执行到return语句,会跳转到finally,在finally语句中遇到了return语句,程序返回。
throws声明异常类型
如果一个方法可能会出现异常,但没有能力处理这种异常,可以在方法中声明处用throws子句来声明抛出异常。
public void method() throws Exception1,Exception2,...,ExceptionN{
//可能产生异常的代码
}
throw在函数内抛出异常,throws可以是异常类的父类。
public void method() throws FatherException1{
//可能产生异常的代码
throw new SonException();
}
自定义异常类
/**
* The class {@code Exception} and its subclasses are a form of
* {@code Throwable} that indicates conditions that a reasonable
* application might want to catch.
*
* <p>The class {@code Exception} and any subclasses that are not also
* subclasses of {@link RuntimeException} are <em>checked
* exceptions</em>. Checked exceptions need to be declared in a
* method or constructor's {@code throws} clause if they can be thrown
* by the execution of the method or constructor and propagate outside
* the method or constructor boundary.
*
* @author Frank Yellin
* @see java.lang.Error
* @jls 11.2 Compile-Time Checking of Exceptions
* @since JDK1.0
*/
public class Exception extends Throwable {
static final long serialVersionUID = -3387516993124229948L;
/**
* Constructs a new exception with {@code null} as its detail message.
* The cause is not initialized, and may subsequently be initialized by a
* call to {@link #initCause}.
*/
public Exception() {
super();
}
/**
* Constructs a new exception with the specified detail message. The
* cause is not initialized, and may subsequently be initialized by
* a call to {@link #initCause}.
*
* @param message the detail message. The detail message is saved for
* later retrieval by the {@link #getMessage()} method.
*/
public Exception(String message) {
super(message);
}
/**
* Constructs a new exception with the specified detail message and
* cause. <p>Note that the detail message associated with
* {@code cause} is <i>not</i> automatically incorporated in
* this exception's detail message.
*
* @param message the detail message (which is saved for later retrieval
* by the {@link #getMessage()} method).
* @param cause the cause (which is saved for later retrieval by the
* {@link #getCause()} method). (A <tt>null</tt> value is
* permitted, and indicates that the cause is nonexistent or
* unknown.)
* @since 1.4
*/
public Exception(String message, Throwable cause) {
super(message, cause);
}
/**
* Constructs a new exception with the specified cause and a detail
* message of <tt>(cause==null ? null : cause.toString())</tt> (which
* typically contains the class and detail message of <tt>cause</tt>).
* This constructor is useful for exceptions that are little more than
* wrappers for other throwables (for example, {@link
* java.security.PrivilegedActionException}).
*
* @param cause the cause (which is saved for later retrieval by the
* {@link #getCause()} method). (A <tt>null</tt> value is
* permitted, and indicates that the cause is nonexistent or
* unknown.)
* @since 1.4
*/
public Exception(Throwable cause) {
super(cause);
}
/**
* Constructs a new exception with the specified detail message,
* cause, suppression enabled or disabled, and writable stack
* trace enabled or disabled.
*
* @param message the detail message.
* @param cause the cause. (A {@code null} value is permitted,
* and indicates that the cause is nonexistent or unknown.)
* @param enableSuppression whether or not suppression is enabled
* or disabled
* @param writableStackTrace whether or not the stack trace should
* be writable
* @since 1.7
*/
protected Exception(String message, Throwable cause,
boolean enableSuppression,
boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
}
public class MyException extends Exception{
String message;
public MyException(String message){
super(message)
}
}
异常链
Modifier | Constructor and Description |
---|---|
| Exception() Constructs a new exception with null as its detail message. |
| Exception(String message) Constructs a new exception with the specified detail message. |
| Exception(String message,Throwable cause) Constructs a new exception with the specified detail message and cause. |
protected | Exception(String message,Throwable cause,boolean enableSuppression,boolean writableStackTrace) Constructs a new exception with the specified detail message, cause, suppression enabled or disabled, and writable stack trace enabled or disabled. |
| Exception(Throwable cause) Constructs a new exception with the specified cause and a detail message of (cause==null ? null : cause.toString()) (which typically contains the class and detail message of cause ). |
class Main{
public void static main(String[] args){
testthree();
}
public static void testone() throws Exception1{
throw new Exception1("抛出第一个异常");
}
public static void testtwo() throws Exception2{
try{
testone();
}catch(Exception e){
throw new Exception("抛出第二个异常");
}
}
public static void testthree() throws Exception3{
try{
testthree();
}
throw new Exception("抛出第三个异常");
}
}
/**
程序大概会输出:
抛出第三个异常
*/
class Main{
public void static main(String[] args){
testthree();
}
public static void testone() throws Exception1{
throw new Exception1("抛出第一个异常");
}
public static void testtwo() throws Exception2{
try{
testone();
}catch(Exception2 e){
throw new Exception("抛出第二个异常",e);
}
}
public static void testthree() throws Exception3{
try{
testthree();
}catch(Exception3 e){
throw new Exception("抛出第三个异常",e);
}
}
}
/**
大概输入内容:
抛出第三个异常
抛出第二个异常
抛出第一个异常
*/