参考链接: Java中ExceptionHandling方法重写
Java - Exceptions Handling
本文参考这里
三类异常:
Checked exceptions:编译时可检查的异常Runtime exceptions:运行时异常Errors:发生错误
异常的体系(Exception Hierarchy)
Throwable
Exception
IOExceptionRuntimeExceptionError
异常的Methods(Exceptions Methods)
略
捕获异常(Catching Exceptions)
try/catch 块
多个catch块(Multiple catch Blocks)
try
{
//Protected code
}
catch (ExceptionType1 e1)
{
//Catch block
}
catch (ExceptionType2 e2)
{
//Catch block
}
catch (ExceptionType3 e3)
{
//Catch block
}
throws/throw 关键字
throws:丢掉已知异常而不处理(not handle a checked exception),放在方法签名的后面throw:抛出异常
public class className
{
public void deposit(double amount) throws RemoteException //丢掉异常
{
// Method implementation
throw new RemoteException(); //抛出异常
}
public void withdraw(double amount) throws RemoteException, InsufficientFundsException //丢掉多个异常不处理
{
// Method implementation
}
//Remainder of class definition
}
finally 关键字
不论try块是否发生异常,finally块总是被执行。
try
{
//Protected code
}
catch (ExceptionType1 e1)
{
//Catch block
}
catch (ExceptionType2 e2)
{
//Catch block
}
finally
{
//The finally block always executes.
}
申明自定的异常(Declaring your own Exception)
所有的exception都是Throwable的孩子(All exceptions must be a child of Throwable)编译时可检查的异常扩展自Exception类运行时异常扩展自RuntimeException类
例如:
class MyException extends Exception
{
}
通用异常(Common Exceptions)
两类异常和错误(Exceptions and Errors)
JVM Exceptions:JVM抛出的异常,比如NullPointerException, ArrayIndexOutOfBoundsException, ClassCastExceptionProgrammatic exceptions:应用或API抛出的异常,比如IllegalArgumentException, IllegalStateException
本文系转载,前往查看
如有侵权,请联系 cloudcommunity@tencent.com 删除。
本文系转载,前往查看
如有侵权,请联系 cloudcommunity@tencent.com 删除。