DCSIMG
August 2010 - Posts - Dotmad (on .Net)

Dotmad (on .Net)

Just Another Web 5.0 Blog

Podcasts

Blogroll

August 2010 - Posts

Possible issues with catching unhandled exceptions in .Net

(This post was previously published in http://dotmad.net/blog/)

 

As a rule, in every application I write, I always implement both the Application.ThreadException and AppDomain.UnhandledException events, to catch exceptions which were not caught by a try..catch block.

There are three reasons behind this:

  1. Present a custom failure message to the user (something like Twitter’s fail whale message) instead of the default ugly (and unclear) error dialog, i.e. “XXX.exe has encountered a problem and needs to close. We are sorry for the inconvenience.
  2. Log the exception so it may be resolved in future releases.
  3. Although it’s not considered as a good practice, some exception may be ignored so the application may keep on running, instead of crashing.

However, I recently came across a case in which the application crashed ungracefully although I implemented those methods. Some research led to me to realize my code is not as full-proof as I thought it was.

The code below will crash your application even if you hooked the two events mentioned above. The UnhandledException event WILL be executed, but afterwards the application will crash anyway, presenting Microsoft’s default error dialog:

private void MyThreadProc()
{
    throw new Exception(”Bad wolf”);
}

private void ClickMe()
{
    Thread myThread = new Thread(new ThreadStart(MyThreadProc));
    myThread.Start();
}

Turns out I was not aware that during the transition between Framework 1.1 and 2.0 (and beyond), Microsoft changed the way the CLR deals with exception that don’t occurs in the application’s main thread, meaning that if you want the application to remain alive even after an external thread exception, you need to tell the CLR to revert to the Framework 1.1 exception handling mechanism, by adding this section to your app.config file:

<configuration
  <
runtime

    <
legacyUnhandledExceptionPolicy enabled=”1″
/> 
  <runtime
>
<configuration>

Again, not a best practice, but if you can’t afford to having

Another footnote is related to using the Application.SetUnhandledExceptionModeMethod. If you try setting it from a Winform application running inside the Visual Studio, you’ll get the following error: “Application exception mode cannot be changed once any Controls are created in the application“, even if that’s the first line of code in the application. This is because the Visual Studio runs a host environment prior to running your application. The methods works just fine when you run it from outside the Visual Studio.

כך תמשכו יותר 'לקוחות' קבועים לבלוג שלכם

קראתי את הפוסט של טל בנוגע לתוסף של חברת Wibiya המספק סרגל כלים של מדיה חברתית לבלוג שלכם, וחשבתי להשלים אותו בהמלצה על תוסף אחר.

משכתם אנשים לבלוג? הגדלתם חשיפה? נהדר. איך תגרמו להם לחזור שוב?

אחת הדרכים היעילה ביותר לשמור על קהל קוראים קבוע לבלוג היא קיום קהילה פעילה של מגיבים. כל בלוגר רוצה לראות הרבה תגובות לפוסטים שלו. לפעמים התגובות אפילו יכולות לייצר ערך מוסף לפוסט שערכו עולה על הפוסט המקורי (בפוסטים הומוריסטיים בד"כ הבדיחות בתגובות טובות יותר מהפוסט עצמו), אז חבל לפספס השקעה במשאב כזה.

לשם כך רצוי לשלב בבלוג מנוע תגובות שמחליף את זה שמובנה כברירת מחדל, אבל מאפשר למגיבים לכתוב תגובות לפוסטים דרך הפרופילים שלהם ברשתות חברתיות כמו פייסבוק או טוויטר.

כלי שכזה הוא DISQUS Comments, פלטפורמה לניהול תגובות בבלוגים, ששוחררה בשנתיים האחרונות:

Disqus Comments is a comment system and moderation tool for your site. This service lets you add next-gen community management and social web integrations to any site on any platform. Hundreds of thousands of sites, from small blogs to large publications, rely on Disqus Comments for their discussion communities.

בשלב זה של הפוסט אני רוצה להבהיר מראש שהוא כנראה אינו תומך ב-Community Server, אז המשך הפוסט רלוונטי בעיקר למי שמנהל בלוגים נוספים בפלטפורמות שונות ורבות. (מצד שני, מפתחי CS עצמם ממליצים עליו)

כמו שתארתי קודם, התוסף הזה מאפשר למגיבים להשתמש בחשבון ה-Facebook Connect, Twitter או OpenID שלהם כדי להגיב. התגובות מוצגות במבנה עץ בדומה לפורומים, כך שניתן להגיב לפוסט, או לתגובה אחרת, עד לקיום דיון שלם בתגובות בלבד.

בנוסף לכך התוסף "שולף" אזכורים של הפוסט מטוויטר ומציג את הדיון בפוסט בטוויטר כהמשך לתגובות, ותומך בדומה גם ב-FriendFeed, Digg או יוטיוב. כך שהדיון בפוסט מתרחב אפילו מעבר לגבולות הבלוג עצמו.

אני משתמש בתוסף הזה בבלוג העברי שלי ומאוד מרוצה ממנו, למרות שחשוב לציין שהוא כבד יחסית, ומאריך את זמן הטעינה של הדפים.