Dear ashokparchuri,
Handling with errors in C# .NET is different than
VB or ASP. C# uses what are called
Exceptions to handle errors. Specifically, the mechanism used is the try, catch and
finally blocks.
If you expect a portion of code to run into some sort of error, you enclose that code in a
try block. Immidiately following the try block, you write a
catch block that catches any thrown exceptions. Finally, following the catch block, you write
finally block. It contains code that you want to be executed whether or not an exception is thrown (error is raised, in old
VB terminology). Here is the general syntax:
Code:
try {
//code that can throw exception goes here...
}
catch(Exception ex) {
MessageBox.Show(ex.Message, "Error");
//code to handle the exception goes here...
}
finally {
//code that you want to be executed whether
//or not an exception is thrown goes here...
}
If you prefer you can omit the finally block or the catch block. But at least one of them has to
be there immidiately following the try block.
I hope this would help. But for a complete coverage of error handling in C# you should consult
a good book, such as C# Step By Step (Microsoft Press) or C# .NET Unleashed (SAMS Publishers).
ejan