PDA

Click to See Complete Forum and Search --> : [2.0] Handling exceptions


JenniferBabe
Jun 23rd, 2007, 01:21 PM
Hi guys, I'm trying to handle an exception but getting some difficulties in its design. Lets say I have a static class:

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:

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


Code in Form2:

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.

ComputerJy
Jun 23rd, 2007, 02:08 PM
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

jmcilhinney
Jun 23rd, 2007, 09:42 PM
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:public static void SomeMethod()
{
SomeMethod(true);
}

public static void SomeMethod(bool suppressExceptions)
{
try
{

}
catch (Exception)
{
if (!suppressExceptions)
{
throw;
}
}
}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.

ComputerJy
Jun 23rd, 2007, 10:29 PM
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?

jmcilhinney
Jun 23rd, 2007, 10:47 PM
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.