
本文共 2008 字,大约阅读时间需要 6 分钟。
try { // Protected code} catch (ExceptionName e1) { // Catch block}
Exceptions Methods
Following is the list of important methods available in the Throwable class.
Sr.No. | Method & Description |
---|---|
1 | public String getMessage() Returns a detailed message about the exception that has occurred. This message is initialized in the Throwable constructor. |
2 | public Throwable getCause() Returns the cause of the exception as represented by a Throwable object. |
3 | public String toString() Returns the name of the class concatenated with the result of getMessage(). |
4 | public void printStackTrace() Prints the result of toString() along with the stack trace to System.err, the error output stream. |
5 | public StackTraceElement [] getStackTrace() Returns an array containing each element on the stack trace. The element at index 0 represents the top of the call stack, and the last element in the array represents the method at the bottom of the call stack. |
6 | public Throwable fillInStackTrace() Fills the stack trace of this Throwable object with the current stack trace, adding to any previous information in the stack trace. |
捕获多个异常
try { // ...} catch(ExceptionType1 e1) { // ...} catch(ExceptionType2 e2) { // ...}
try { // ...} catch(ExceptionType1|ExceptionType2 ex) { // since Java 7 // ...}
Throws / Throw 关键字
- If a method does not handle a checked exception, the method must declare it using the throws keyword. The throws keyword appears at the end of a method's signature.
- You can throw an exception, either a newly instantiated one or an exception that you just caught, by using the throw keyword.
import java.io.*;public class ClassName { public void withdraw() throws RemoteException, InsufficientFundsException { // Method implementation } // Remainder of class definition}
The finally block
The finally block follows a try block or a catch block. A finally block of code always executes, irrespective of occurrence of an Exception.
安利一门深度学习课程
扫码下单输优惠码【csdnfxzs】再减5元,比官网还便宜!发表评论
最新留言
关于作者
