Exception handling in WinForm
I am beginner in C# programming. I have encountered small problem during building application using form. I will try to explain it properly within my abilities and experience. Problem uccured when i tried to handle exception caused by Class1 instantialized in my Form1. Let say i have function "public int Calc(i开发者_如何学Cnt a, int b)" in Class1. In Form1 i have instantionalized this class to call its "Calc" function. If i want to message an error (f.e: divided by zero) i have to wrap function call into try/catch elements:
//Form1:
Class1 C1 = new Class1();
int a = 5;
int b = 0;
int c = 0;
try{
c = C1.Calc(a,b)
}
catch(DivideByZeroException e)
{
// some error handling code
}
... i think this example is not proper OOP technique so i had to decide to put try/catch elements directly into Class1:
//Class1:
public int Calc(int a, int b)
{
int c = 0;
try{
c = a/b;
}
catch(DivideByZeroException e)
{
// .........
}
return c;
}
... question is, how can i get message (DivideByZeroException e) into my Form1 to be able to handle it and message it. I don't want to create some static function in Form1 just to reach MessageBox class in it from Class1 because it does not make sence in terms of proper OOP functionality and reusability of Class1. I have read about events and delegates (which i understand is simple pointer to function similar to C++) but it is somehow confusing and i failed to apply this technique into my code. Could you please write simple example which would point me to right direction.
thank You guys
Cembo
The proper technique really is the first one. If you can't handle it inside your function, then you have no business trying. Put the exception handling in a place where the exception can be handled and the program can continue (or exit gracefully) and the user be notified of the error in an appropriate fashion.
I suggest you implement following code in your app inside the Program.cs
file
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
// -----------------
Application.ThreadException += Application_ThreadException;
// -----------------
}
static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
{
// Handle your exception here...
MessageBox.Show(string.Format("There is an error\n{0}", e.Exception.Message));
}
this will catch your unhandled exception in entire application.
In order for it to bubble up into the scope of the calling UI, you would need to throw the exception once caught, or better yet, don't throw it at all - since dividing by zero will cause the CLR to throw the exception if necessary. You could do simple checks here, while the Calc
API call could still throw an exception.
For example, just check the data prior to the Calc
call:
if (a > 0 && b > 0)
{
var result = Calc(a, b);
}
else
{
//tell the user to input valid data
}
In the Calc
method you could do a similar check and throw a relevant exception:
public int Calc(int a, int b)
{
if (a <= 0) throw new ArgumentException("appropriate message here");
if (b <= 0) throw new ArgumentException("appropriate message here");
...
}
the idea here is prevention of dividing by zero, but it might be a little excessive in your case, as the earlier example shows you can essentially provide the same behaviour, however now you need to catch the exception:
try
{
var result = Calc(a, b);
}
catch //use appropriate exception catches
{
//tell the user to input valid data
}
public int Calc(int a, int b)
{
int c = a/b;
}
Leave it plain and simple with whataver logic you need.
Then handle the error within the form how you did it in the first place.
One simple way could be to change your method in to something like this
public int Calc(int a, int b, out string error)
{
error = string.empty;
int c = 0;
try
{
c = a/b;
}
catch(DivideByZeroException e)
{
error = e.ToString();
}
return c;
}
Now in the caller you can check
string error;
int res = Calc( 1, 2, out error);
if(!string.isnullorempty(error)
{
//error has occured
messagebox.show(error);
}
else
{
//no error
// you can proceed noramlly
}
精彩评论