为什么不用SEH
前些时候,在论坛上看到一个朋友说SEH怎么怎么不好,一定不要用它。其实存在即合理。就像GOTO,不能因为它破坏了程序的流程,就不用它,适当的使用,还是可以事半功倍的。
大家知道SEH是Windows操作系统提供的一种异常处理机制,它和C++无关。在Compiler编译的时候,就把这个机制加入了我们的程序中。在VC下可以用__try, __finally, __except, __leave等关键字来标识。由于SEH可以捕获硬件异常(Hardware Exception)和软件异常(Software Exception),它比C++的异常机制能捕获更多的异常,所以有朋友不喜欢这点,认为它掩盖了错误。其实这种说法是也是合情合理的,毕竟掩盖错误不是最好的解决方案,找出问题的所在才是我们应该做的。可是在现实中,我们不可能找到所有的bug,或者由于时间的关系,来不及修补这个bug,不如先用SEH挡一挡,何尝不可。
就像我之前的一个项目,程序在一个地方偶尔会Crash掉,而且这个地方如果不能正常执行丝毫不影响整个程序的运作,不会对用户造成损失,在找出问题真正的原因之前,我们完全可以用SEH捕获异常。
下面的例子也是一个SEH优势的体现
BOOL SafeDiv(INT32 dividend, INT32 divisor, INT32 *pResult)
{
__try
{
*pResult = dividend / divisor;
}
__except(GetExceptionCode() == EXCEPTION_INT_DIVIDE_BY_ZERO ?
EXCEPTION_EXECUTE_HANDLER : EXCEPTION_CONTINUE_SEARCH)
{
return FALSE;
}
return TRUE;
}
SafeDiv用来做除法操作,它的返回值指出函数是否执行成功。pResult指向最终的结果,如果不用SEH,这类Hardware Exception会导致程序Crash,这里引入了SEH,我们在发现有除零错误的时候,让函数返回FALSE,调用处通过检查函数的返回值就可以判断除法运算是否成功,没必要就因为一个除零错误导致程序Crash掉。
由于SEH是Windows操作系统特有的机制,所以它不适合用在那些跨平台的代码,这种情况不用也罢,既然SEH是一个Windows的一个很好的异常处理机制,我们虽不能滥用它,适当的、合理的使用还是值得推荐的。
P.S.
For details about SEH, check this: http://www.google.com/search?hl=en&q=Programming+Applications+for+Microsoft+Windows
A New Release of Yet Another Browser
I've changed my plan, Yet Another Browser(YAB for short) still looks like its sibling, if a brower has a MS-Word-like look-and-feel, is it weird? So far, I think YAB will be helpful for some guys. If you're addicted to online photo communities, like Flickr, it can help you download photos when you go surfing, you don't need to save the photos manually. Just one click, all photos are flying to your disk on autopilot.
Features:
- You can tune up the opacity of main window
- Multi-tab support
- Right-click Close-Button to click all tabs, Left-Click just close one.
- Your can rename the title of YAB, it will show what you want on task bar.
- You can set the length of tab's title.
- Keep a list of websites you recently closed, you can easily undo it.
- Just one-click to download all images in current tab
- You can change the default direcotry where images will be saved.
Todo:
- Boss key
- Regular-Expressions for downloading images
- Password Protection
- Favorites
- Clear hisotry
- Clear dropdown list
- Self-update
- i18n
- Auto-downloading images when you go surfing, even you don't need one-click.
- You can replace the icon of YAB with another one.
Homepage for YAB: http://charry.org/m/myapps/YetAnotherBrowser
Stay tuned.
TabIndex != TabPages.IndexOf
TabControl的有个属性叫TabIndex,每个从Control派生来的控件都有这个属性,这个Tab是Tab键的顺序(MSDN说:A control with a lower tab index receives focus before a control with a higher index.)
我正好需要获取TabControl中某个Tab Page的Index。这些Tab Page保存在TabControl的TabPages中,它是一个集合,实现了IList接口,其中有个方法就是IndexOf (Determines the index of a specific item in the IList.)。
鬼使神差的,我把TabIndex属性当成了Tab Page在TabPages中的Index了,花费了N久时间,才发现问题所在。Tab Index和IndexOf,TabPages,这几个词放在一起,一不小心就会搞错,错误总在不经意间。
Yet Another Browser
If you're a office worker, are you afraid of being busted by your boss when surfing? I make this semi-browser from scratch, it resembles a regular Microsoft Office Suit Application, I'll add the following features to the browser, e.g.:
- Transparent window
- One-click to hide
- Download all images to your disk
- ...
I created it with C#, it's easy to create your own browser with its powerful components, everyone can make it if he wishes.
Download it! (Make sure you have the .NET Framework 2.0 installed on your computer)