简体   繁体   中英

Why multiple try..catch block not working properly in same method in c# 2.0?

I am doing this in the Form_Load() event of a desktop application

string strDay = DateTime.Now.DayOfWeek.ToString().ToUpper();

            try
            {
                fnBirthDayReminder();
            }
            catch (Exception ex)
            {
            }

            try
            {
                if (strDay == "SUNDAY" || strDay == "TUESDAY" || strDay == "THURSDAY")
                {
                    fnAwaitingLeaveApplicationReminder();
                }
            }
            catch (Exception ex)
            {
            }

            try
            {
                fnLeavePlanRemainder();
            }
            catch (Exception ex)
            {
            }

            try
            {
                fnContractExpiryRemainder();
            }
            catch (Exception ex)
            {
            }

            Application.Exit();

But the application exists just after the execution of the first try..catch block. Even if I place BreakPoint on following try..catch's, these breakpoints were not hit. I am really confused about such mysterious behavior. Please help !

For you all, " if one method throws an exception, the other methods will not run. " this is the main reason I am using separate try..catch blocks. So that, even if a function gives an exception, the next can execute.

Edit2
Can you suggest me a nice approach other than I am using here to execute the next function even if an exception occurred during the first function. The way some of you are suggesting (calling all the functions in a single try block with multiple catch blocks) will not do, that's for sure. I am thinking about recoding the methods without spending more time.

Have you tried putting a breakpoint in the first catch block, and examining the exception message / stack trace? I've observed sometimes that the application can exit for certain types of exception, eg stackoverflow, rather than the expected behaviour.

            try
            {
                fnBirthDayReminder();
            }
            catch (Exception ex)
            {
                Debugger.Break();
            }

Finally, your methods shouldn't throw under normal circumstances. Try to find out why they are and remove the bugs.

Using such a code is not recommended. Try to use one try block and catch the exceptions in multiple catch blocks.

 try
 {
    fnBirthDayReminder();
    fnLeavePlanRemainder();
    fnContractExpiryRemainder();
    //...
 }
 catch(IOException ex)
 {
    //do something
 }

 //catch(...)

 catch(Exception ex)
 {
    //do something
 }

PS: in this sample if one method throws an exception, the other methods will not run.

PS2: The order of catch blocks changed. (Thanks to @ChrisF)

Put a breakpoint on Application.Exit(); and watch is it hit after throwing an exception or not.

If breakpoint not hit then your solution is clear. In this case your code throwing a ThreadException or UnhandedException that force your program to close unexpectedly.

You can catch these exception by doing something like this in your Program.cs to avoid your app close unexpectedly:

static void Main()
{
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
    try
    {
        Application.Run(new Form1());
    }
    catch (Exception e)
    {
        HandleException(e);
    }

static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
{
    HandleException(e.Exception);
}

static void HandleException(Exception ex)
{
    //Do Something
}

Pretty sure you need a finally in there as a try can have multiple catches, but not multiple "trys" within the context of the function.

try { myfunc(); }
catch(Exception ex) { doSomething(); }
finally {}
//time for next try

Indeed, the standard way of using try-catch block is to use multiple catch block with one try block .....

        try
        {
            fnBirthDayReminder();
            if (strDay == "SUNDAY" || strDay == "TUESDAY" || strDay == "THURSDAY")
            {
                fnAwaitingLeaveApplicationReminder();
            }
            fnLeavePlanRemainder();
            fnContractExpiryRemainder();
        }
        catch (Exception ex)
        { 

        }
        Application.Exit();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM