Custom Exception in C# windows application

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • hsachdevah
    New Member
    • Nov 2008
    • 20

    Custom Exception in C# windows application

    Hello,
    I have a c# windows application and I am using a try catch block to handle some exception.
    I want to show show error in messagebox. but the problem is that program execution does not stop there.
    If I use 'throw new exception', I get that ugly windows default msg box.

    any solutions?

    Code:
    try
    {
    ...code
    }
    catch (Exception)
    {
        MessageBox.Show("Error: Invalid Parameters" + Environment.NewLine + "Enter correct values", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
    Last edited by tlhintoq; Jun 23 '09, 06:32 PM. Reason: [CODE] ... your code here ... [/CODE] tags added
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    What do you mean by
    but the problem is that program execution does not stop there.
    ?

    Are you saying you don't get an exception unless you explicitly throw it?

    Maybe you could provide the actual code you are testing for inside the 'try' block.

    You may also be interested in something like this...

    Code:
    try
    {
       // Code that breaks goes here
    }
    catch(Exception err)
    {
       MessageBox.Show(err.message, "error");//overload as you like
    }
    ...where the box dynamically includes the actual error message.

    Comment

    • hsachdevah
      New Member
      • Nov 2008
      • 20

      #3
      here are the details:

      1. the control comes to the catch block.
      2. custom message appears on the screen.
      3. when I press OK on the message window. the control moves though the catch block and executes other statements after that.

      but I want that the program execution should stop after catch block statements are done.

      Comment

      • tlhintoq
        Recognized Expert Specialist
        • Mar 2008
        • 3532

        #4
        Oh... You can return out of the function.

        Code:
        try
        {
           // Code that breaks goes here
        }
        catch(Exception err)
        {
           MessageBox.Show(err.message, "error");//overload as you like
           return;// Leave this method and return to where it was called.
        }
        But might I suggest you read up on the ErrorProvider component? I sounds like that might be more along the lines of your final goal.

        Comment

        • hsachdevah
          New Member
          • Nov 2008
          • 20

          #5
          thanks.
          i'll check on that

          Originally posted by tlhintoq
          Oh... You can return out of the function.

          .

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            Originally posted by hsachdevah
            here are the details:

            1. the control comes to the catch block.
            2. custom message appears on the screen.
            3. when I press OK on the message window. the control moves though the catch block and executes other statements after that.

            but I want that the program execution should stop after catch block statements are done.
            That is exactly how the program should behave. Providing the catch block means that you have handled the exception so the program will not crash.
            If you want to program to exit after the user has clicked OK then provide the exit statement in the catch block. If you want the method to stop executing at that point then put a return statement in that catch block.

            Comment

            Working...