none
how to stop execution after catch block? RRS feed

  • Question

  • Hello Everybody,

    i have try catch block in a windows based application

    try
    {
    Submit();
    }
    catch
    {
    MessageBox.Show("Error in Submit");
    }

    ClearAllTextboxes();

    how do i make sure that the ClearAllTextboxes() method should not be executed, if there is an error in the Submit method and the exception was caught by the catch.

    The user should just get the error message and should be able to make appropriate changes in the textboxes and submit back.

    rite now it shows the message but the method ClearAllTextboxes() gets executed :(


    Thanks

    • Edited by CodeSri Monday, December 15, 2008 9:14 PM afa
    Monday, December 15, 2008 9:08 PM

Answers

  • Insert a "return;" statement in the catch block after the message box. This will break out of the method.
    -Rob Teixeira
    • Marked as answer by CodeSri Monday, December 15, 2008 10:58 PM
    Monday, December 15, 2008 9:59 PM

All replies

  • Insert a "return;" statement in the catch block after the message box. This will break out of the method.
    -Rob Teixeira
    • Marked as answer by CodeSri Monday, December 15, 2008 10:58 PM
    Monday, December 15, 2008 9:59 PM
  • Thank you ...
    my whole class is actually like this...

    insertmethod()
    {

    submitalldata();

    clearalltextboxes();

    }


    submitalldata()
    {
    try
    {
    Submit();
    }
    catch
    {
    MessageBox.Show("Error in Submit");
    return;
    }
    }

    it comes out of the submitalldata() method but still executes clearalltextboxes() method

    wat can i do to stop it from executing?

    Monday, December 15, 2008 10:06 PM
  • Just do it like this? unless the Submit function succeeds it will never ht the ClearTextBoxes function.

     try
    {
    Submit();
    ClearAllTextboxes();
    }
    catch
    {
    MessageBox.Show("Error in Submit");
    }

    Monday, December 15, 2008 10:41 PM
  • Thank you... i did it in the following way..

    thanks for ur help Ray :)


    insertmethod()
    {

    try
    {
    submitalldata();
    }
    catch(Exception ex)
    {
    return;
    }

    clearalltextboxes();

    }


    submitalldata()
    {
    try
    {
    Submit();
    }
    catch
    {
    MessageBox.Show("Error in Submit");
    throw ex;
    }
    }
    Monday, December 15, 2008 11:00 PM
  •  Although both pieces of code seem to do the same thing (excpet for the rethrow in yours), be honest which one is easier to read (and maintain when you look at the code 4 weeks from now)?

    This :

    try 
    {  
        Submit();  
        ClearAllTextboxes();  
    }  
    catch 
    {  
        MessageBox.Show("Error in Submit");  

    or this:

    insertmethod()  
    {  
       try 
       {  
          submitalldata();  
       }  
       catch(Exception ex)  
       {  
          return;  
       }  
       clearalltextboxes();  
    }  
     
     
    submitalldata()  
    {  
       try 
       {  
          Submit();  
       }  
       catch 
       {  
          MessageBox.Show("Error in Submit");  
          throw ex;  
       }  


    Monday, December 15, 2008 11:07 PM
  • Note that it's better to write 'throw;' instead of 'throw ex;'. Use the following pattern in retrowing exceptions:

    try    
    {    
        Submit();    
    }    
    catch    
    {    
        MessageBox.Show("Error in Submit" );    
        throw // no 'throw ex' here.  
    }  

    The reason is that you will loose the stack trace when calling 'throw ex', which will hide the original location where the exception was first thrown.

    Good luck

    Visit my blog: http://www.cuttingedge.it/blogs/steven/
    Tuesday, December 16, 2008 7:48 PM