C / C++的和Java的异常机制
要注意一点:异常机制处理异常是要付出代价的,即异常处理的代码比无异常处理的要慢好多倍。
![]()
public class Throwable implements Serializable ...{![]()
/** *//** use serialVersiborder="0" align="top" alt="C / C++的和Java的异常机制(图六)" width="11" height="16" /> private static final lborder="0" align="top" alt="C / C++的和Java的异常机制(图六)" width="11" height="16" />![]()
/** *//**
* Native code saves some indicatiborder="0" align="top" alt="C / C++的和Java的异常机制(图七)" width="11" height="16" /> */
private transient Object backtrace;
private String detailMessage;
private Throwable cause = this;
private StackTraceElement[] stackTrace;
![]()
public Throwable() ...{
fillInStackTrace();
}![]()
![]()
public Throwable(String message) ...{
fillInStackTrace();
detailMessage = message;
}![]()
![]()
public Throwable(String message, Throwable cause) ...{
fillInStackTrace();
detailMessage = message;
this.cause = cause;
}![]()
![]()
public String getLocalizedMessage() ...{
return getMessage();
}![]()
![]()
public Throwable getCause() ...{
return (cause==this ? null : cause);
}![]()
![]()
public synchrborder="0" align="top" alt="C / C++的和Java的异常机制(图六)" width="11" height="16" /> if (this.cause != this)
throw new IllegalStateExceptiborder="0" align="top" alt="C / C++的和Java的异常机制(图六)" width="11" height="16" /> if (cause == this)
throw new IllegalArgumentExceptiborder="0" align="top" alt="C / C++的和Java的异常机制(图六)" width="11" height="16" /> this.cause = cause;
return this;
}![]()
![]()
public String toString() ...{
String s = getClass().getName();
String message = getLocalizedMessage();
return (message != null) ? (s + ": " + message) : s;
}![]()
![]()
private synchrborder="0" align="top" alt="C / C++的和Java的异常机制(图六)" width="11" height="16" /> // Initialize stack trace if this is the first call to this method![]()
if (stackTrace == null) ...{
int depth = getStackTraceDepth();
stackTrace = new StackTraceElement[depth];
for (int i=0; i < depth; i++)
stackTrace[i] = getStackTraceElement(i);
}
return stackTrace;
}
//......省略了一些
}![]()
注意一点:异常类是可串行化的。
public class Exceptiborder="0" align="top" alt="C / C++的和Java的异常机制(图一)" width="11" height="16" />- ·
如果可能发生多种异常时,可用多个catch语句捕捉不同类型的异常,从第一个catch开始匹配异常,如果异常是该类或该类的子类,则匹配。如果要匹配所有的异常,则在catch中捕捉
using namespace std;![]()
#ifndef NULL
#define NULL 0
#endif
class MyExceptiborder="0" align="top" alt="C / C++的和Java的异常机制(图二)" width="11" height="16" />
...{
cborder="0" align="top" alt="C / C++的和Java的异常机制(图六)" width="11" height="16" /> public:![]()
MyExceptiborder="0" align="top" alt="C / C++的和Java的异常机制(图六)" width="11" height="16" /> void print()![]()
...{
cout<<msg<<endl;
}
};
void f()![]()
...{
throw MyExceptiborder="0" align="top" alt="C / C++的和Java的异常机制(图八)" width="11" height="16" />}![]()
int main()![]()
...{
try![]()
...{
f();
}catch(MyExceptiborder="0" align="top" alt="C / C++的和Java的异常机制(图四)" width="11" height="16" />
...{
me.print();
}
system("pause");
return 0;
}
C++的异常捕捉匹配和JAVA的基本相同,只是C++没有 finally 块。要捕捉所有异常的方法是用 catch(...) 语句。
以上所述都只是JAVA和C++的异常机制的皮毛。对JAVA的异常,觉得自己理解得还可以,懂得什么是捕捉,什么是抛出异常、传播异常和包装异常等。但是对C++的异常,可以说是刚刚接触,刚才看了 Thinking in C++ 异常处理的一章。想起JAVA的异常机制,就作个对比,写个笔记。
- 最新文章
- 深入理解C++中的mutable关键字[01-03]
- C++中通过重载避免隐式类型转换[01-03]
- Awk 基础入门:Awk 实例编程[01-03]
- C++类对象的拷贝构造函数分析[01-03]
- C程序中外部变量与函数关系解惑[01-03]
- 实现C语言高效编程的四大秘技[01-03]
- 相关文章
- 从C++转到Java 如何把握关键[01-03]
- Java语言和C++语言的差异[01-03]
- 管理 Java 类路径(UNIX 和 Mac OS X)[01-03]
- 大型Java Web系统服务器选型问题探讨[01-02]
