Warning: The magic method Math_Captcha::__wakeup() must have public visibility in /home/wwwroot/blog.gotocoding.com/wp-content/plugins/wp-math-captcha/wp-math-captcha.php on line 87

Warning: Cannot modify header information - headers already sent by (output started at /home/wwwroot/blog.gotocoding.com/wp-content/plugins/wp-math-captcha/wp-math-captcha.php:87) in /home/wwwroot/blog.gotocoding.com/wp-content/plugins/wp-math-captcha/includes/class-cookie-session.php on line 46

Warning: Cannot modify header information - headers already sent by (output started at /home/wwwroot/blog.gotocoding.com/wp-content/plugins/wp-math-captcha/wp-math-captcha.php:87) in /home/wwwroot/blog.gotocoding.com/wp-content/plugins/wp-math-captcha/includes/class-cookie-session.php on line 49

Warning: Cannot modify header information - headers already sent by (output started at /home/wwwroot/blog.gotocoding.com/wp-content/plugins/wp-math-captcha/wp-math-captcha.php:87) in /home/wwwroot/blog.gotocoding.com/wp-content/plugins/wp-math-captcha/includes/class-cookie-session.php on line 49

Warning: Cannot modify header information - headers already sent by (output started at /home/wwwroot/blog.gotocoding.com/wp-content/plugins/wp-math-captcha/wp-math-captcha.php:87) in /home/wwwroot/blog.gotocoding.com/wp-content/plugins/wp-math-captcha/includes/class-cookie-session.php on line 49

Warning: Cannot modify header information - headers already sent by (output started at /home/wwwroot/blog.gotocoding.com/wp-content/plugins/wp-math-captcha/wp-math-captcha.php:87) in /home/wwwroot/blog.gotocoding.com/wp-content/plugins/wp-math-captcha/includes/class-cookie-session.php on line 49

Warning: Cannot modify header information - headers already sent by (output started at /home/wwwroot/blog.gotocoding.com/wp-content/plugins/wp-math-captcha/wp-math-captcha.php:87) in /home/wwwroot/blog.gotocoding.com/wp-content/plugins/wp-math-captcha/includes/class-cookie-session.php on line 49

Warning: Undefined array key "action" in /home/wwwroot/blog.gotocoding.com/wp-content/plugins/wp-recentcomments/core.php on line 7

Warning: Undefined array key "action" in /home/wwwroot/blog.gotocoding.com/wp-content/plugins/wp-recentcomments/core.php on line 12

Warning: Cannot modify header information - headers already sent by (output started at /home/wwwroot/blog.gotocoding.com/wp-content/plugins/wp-math-captcha/wp-math-captcha.php:87) in /home/wwwroot/blog.gotocoding.com/wp-content/plugins/all-in-one-seo-pack/app/Common/Meta/Robots.php on line 89

Warning: Cannot modify header information - headers already sent by (output started at /home/wwwroot/blog.gotocoding.com/wp-content/plugins/wp-math-captcha/wp-math-captcha.php:87) in /home/wwwroot/blog.gotocoding.com/wp-includes/feed-rss2.php on line 8
exception | 重归混沌的BLOG https://blog.gotocoding.com 师法天地, 道法自然 Fri, 01 May 2015 08:13:32 +0000 zh-Hans hourly 1 https://wordpress.org/?v=6.9 异常机制 https://blog.gotocoding.com/archives/268?utm_source=rss&utm_medium=rss&utm_campaign=exception https://blog.gotocoding.com/archives/268#comments Tue, 28 Oct 2014 13:15:58 +0000 http://blog.gotocoding.com/?p=268 阅读更多]]> 记得学习C++之初就接触到, C++有异常机制, 可是我就是不知道为什么会有异常, 常常在想有返回值不是就够了, 就此问题我也问了很多人, 找了很多书, 可是只是有人给我说异常怎么用, 却就是不给我说异常为什么产生的, 当然有可能是我没精读过大多C++书籍导致的.

后来转向C之后, 这个问题也就被搁置了. 直到不久前, 用C的返回值写的很烦时(几乎每个函数调用都需要检查返回值), 又想到了C++的异常机制, 终于发现异常机制的方便之处.

使用异常几乎可以省掉大半的检查返回值的代码, 当然这并非不检查, 只是在语法上可以很掉很大的功夫, 这也难怪”C接口与实现”的作者去花那么大力气去实现一个异常库了.

自然语言的描述总是缺乏说服力, 机器的世界还是用机器语言来描述:


//C++ language

#include
int func1(int n)
{
if (n == -1)
throw 3;
printf("func1: successn");
return 0;
}
int func2(int n)
{
func1(n);
printf("func2: successn");
return 0;
}

int main(int argc, char * argv[])
{
try {
func2(-1);
} catch (int n) {
printf("exception:%dn", n);
}

return 0;
}

//C language
#include

int func1(int n)
{
if (n == -1)
return 3;
printf("func1: successn");

return 0;
}

int func2(int n)
{
int err;

err = func1(n);
if (err != 0)
return err;

printf("func2: successn");

return 0;
}

int main(int argc, char * argv[])
{
int err;

err = func2(-1);

if (err != 0)
printf("exception:%dn", err);

return 0;
}

比较C与C++的代码可以发现, 在没有使用异常的代码中(C代码), 每一个层调用, 每一个函数的返回值都要去判断, 函数体几乎要爆增一倍. 而使用异常的代码测可以在throw之后直接一路飞速回到最上层想要检查返回值的地方. 可以省下大规模的返回值比较代码, 我想这也许就是异常机制出现的意义吧.

btw, 其实异常机制就是语法糖, 只不过是很实用的语法糖.

]]>
https://blog.gotocoding.com/archives/268/feed 1