Results 1 to 5 of 5

Thread: [2.0] Handling exceptions

  1. #1

    Thread Starter
    Hyperactive Member drattansingh's Avatar
    Join Date
    Sep 2005
    Posts
    395

    [2.0] Handling exceptions

    Hi guys, I'm trying to handle an exception but getting some difficulties in its design. Lets say I have a static class:

    Code:
    class StaticQuery
    {
         public static void method1()
         {
              try
               {
                        // some code here
    
    
               } // end try
               catch(InvalidOperationException e1)
               {
                     // Some error handling code here
    
                }
    
    
          }// end method
    
    }// end class.

    The above works fine, if some code in the try clause generates an Invalidoperation exception, the catch clausee will kick in and execute.

    My problem is when using this class. Let's say I have 2 forms: Form1, Form2 (this is just a small example but it highlights my problem).

    Code in Form1:

    Code:
    try
    {
           StaticQuery.method1
    }
    catch(Invalidoperationexception)
    {
          MessageBox.show("A certain message");
    }
    Code in Form2:

    Code:
    try
    {
           StaticQuery.method1
    }
    catch(Invalidoperationexception)
    {
          MessageBox.show("A different message will be displayed.");
    }


    So in another class or a form, I"m calling this method and when the invalidoperationexception is thrown, I want it to be handled by that form (form1, form2) and not the static class (staticquery). But its not like I could take it out of the static class since there are many other classes that use the method and needs that invalidoperationexception.

    I hope I explained myself clearly, could anyone help me, I think its a problem of design, but I could be wrong.

    Jennifer.

  2. #2
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: [2.0] Handling exceptions

    Exceptions are call stack recursive, in other words Exceptions will be sent back to the original caller until a catch block is found (try block surrounding the exception thrower of course) and If it doesn't find, the .NET framework will catch the exception and show it's usual message box.

    So you can surround any code that might throw this exception in the Form1 or Form2 with a try-catch block(The form has to be a parent caller of the method). Then you can handle the exception in whatever way you want
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2.0] Handling exceptions

    First of all that is not a static class. A class is not static unless it has the "static" key word in its declaration. The fact that it has no instance methods doesn't stop you creating an instance. If you add the "static" key word to the declaration then no instance can be created. Static classes are functionally equivalent to modules in VB.NET.

    As to your issue, you could do something like this:
    C# Code:
    1. public static void SomeMethod()
    2. {
    3.     SomeMethod(true);
    4. }
    5.  
    6. public static void SomeMethod(bool suppressExceptions)
    7. {
    8.     try
    9.     {
    10.  
    11.     }
    12.     catch (Exception)
    13.     {
    14.         if (!suppressExceptions)
    15.         {
    16.             throw;                    
    17.         }
    18.     }
    19. }
    Now your existing code that calls the method without a parameter will continue to execute it with exceptions caught internally, while these forms that want to have the exceptions thrown up can simply pass 'false' as a parameter.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: [2.0] Handling exceptions

    Quote Originally Posted by jmcilhinney
    First of all that is not a static class. A class is not static unless it has the "static" key word in its declaration. The fact that it has no instance methods doesn't stop you creating an instance. If you add the "static" key word to the declaration then no instance can be created. Static classes are functionally equivalent to modules in VB.NET.
    I thought it's called abstract classes. Or are they different?
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2.0] Handling exceptions

    Quote Originally Posted by ComputerJy
    I thought it's called abstract classes. Or are they different?
    An abstract class cannot be instantiated directly but it can have instance members. An abstract class is one that must be inherited in order to created an instance. Instances of derived classes can invoke the instance members of the abstract base class.

    A static class can also not be instantiated, but neither can it be inherited. A static class can have only static members, but unless the class itself is declared static then you can still create an instance.

    As always, the MSDN documentation explains what an abstract class is and what a static class is.
    Last edited by jmcilhinney; Jun 23rd, 2007 at 10:54 PM.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width