📄 exceptionspecifications.html
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<link rel="stylesheet" href="css/stdlayout.css" type="text/css">
<link rel="stylesheet" href="css/print.css" type="text/css">
<meta content="text/html; charset=gb2312" http-equiv="content-type">
<title>例外规格(Exception Specifications)</title>
</head>
<body>
<h3><a href="http://caterpillar.onlyfun.net/GossipCN/index.html">From
Gossip@caterpillar</a></h3>
<h1><a href="CppGossip.html">C++
Gossip: 例外规格(Exception Specifications)</a></h1>
例外规格(Exception
Specifications)指的是在函式定义时,宣告其将丢回某些类型的例外,当您宣告一个函式将丢回例外物件时,通常表示该函式中发生某些类型例外
时会将之丢出(函式中可能不处理,或是处理完后重新丢出),呼叫该函式的函式必须负责处理它所丢出的例外,另一方面,您也只能丢出函式上所指定的例外,这
意味着除了指定的例外之后,其它类型的例外您必须在函式中加以处理。<br>
<br>
要宣告一个函式丢出例外,其语法如下: <br>
<div style="margin-left: 40px; font-family: Courier New,Courier,monospace;"><span style="font-weight: bold;">return-type function throw (type1, type2, type3) { </span><br style="font-weight: bold;">
<span style="font-weight: bold;"> // .... </span><br style="font-weight: bold;">
<span style="font-weight: bold;"> throw type1; </span><br style="font-weight: bold;">
<span style="font-weight: bold;"> // .... </span><br style="font-weight: bold;">
<span style="font-weight: bold;"> throw type2; </span><br style="font-weight: bold;">
<span style="font-weight: bold;"> // .... </span><br style="font-weight: bold;">
<span style="font-weight: bold;">}</span><br>
</div>
<br>
例外规格是函式定义的一部份,函式宣告式与定义式必须都写出来,例如:<br>
<div style="margin-left: 40px;"><span style="font-weight: bold; font-family: Courier New,Courier,monospace;">void divide(int, int) throw(DividedByZero); </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">void divide(int a, int b) throw(DividedByZero) { </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;"> ....</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">}</span><br>
</div>
<br>
例外的丢出是在执行时期引发的,当在执行时期丢出未列于例外规格中的例外时,unexpected()会被呼叫执行,它预设会呼叫terminate()函式,结果就是终止程式。<br>
<br>
如果想限制函式不丢出任何类型的例外,则在函式定义时使用throw,但不指定任何类型的例外,例如:<br>
<div style="margin-left: 40px;"><span style="font-weight: bold; font-family: Courier New,Courier,monospace;">void function() throw() {</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;"> ....</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">}</span><br>
</div>
<br>
如此一来,所有的例外都不能从函式中丢出,所以例外的发生都必须在函式中捕捉并处理完毕。<br>
<br>
在定义函式指标时,如果函式上有宣告例外规格,则例外规格也必须一并写出,例如宣告一个可指向前例中divide的函式指标:<br>
<div style="margin-left: 40px;"><span style="font-weight: bold; font-family: Courier New,Courier,monospace;">void (*fptr)(int, int) throw(DividedByZero);</span><br>
</div>
<br>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -