Custom .NET Exceptions
The .NET Framework offers programmers a familiar try...catch construct for coping with errors, or exceptions, that arrise duiring run time. The .NET Framework offers a selection of standard Exception classes that deal with some generic situations, such as the ArithmeticException and NullReferenceException classes. These are useful on their own, but when you are developing large, low-coupled systems sometimes you need more.Fortunately the .NET Framework allows us to define and use our own custom exceptions. These are actually just classes that derive from the ApplicationException class, so its actually very very simple. Here is am example custom exception that I've created to deal with some hypothetical situation where there is no more tickets left in some imaginary ticket ordering system
public class NoTicketsLeftException : ApplicationException {
public NoTicketsLeftException() {
}
public NoTicketsLeftException(String message) : base(message) {
}
public NoTicketsLeftException(String message, Exception inner) : base(message, inner) {
}
}
So here we've got a small little class deriving from the ApplicationException
class, containing 3 methods with empty bodies. That's all we need
here - we dont need to do anything else if we dont want to - simple huh?So how do we use this in code? Lets think about the imaginary ticket ordering system again - if the user tries to order a ticket and there are none left, they should be shown an message telling them. Lets have a look at some code for the method that will throw our custom exception.
public Boolean BuyTickets(Int32 TicketsToBuy) {
// ... Here we'd do some sort of database lookup, returning the remaining number of tickets.
if (TicketsLeft == 0) {
// Uh oh! no more tickets...time to throw our custom exception!
throw(new NoTicketsLeftException());
} else {
// Ok there were some tickets left... do the rest and return to the user.
}
}
Ok so we can throw our exception now; this is how we catch it.
public void TicketOrdering () {
// ... here we'd deal with the user input verification etc
try {
. . .
Boolean BuyTicketsResult = BuyTickets(TicketsRequired);
. . .
} catch (NoTicketsLeftException ex1) {
// Ok there weren't any tickets left! Let the user know.
MessageBox.Show("There are no tickets left. Please select a different day.");
} catch (Exception ex2) {
// There was some other exception raised, such as a database error for example.
MessageBox.Show("Something broke. Please seek technical assistance!");
}
}
So here if the BuyTickets method throws the NoTicketsLeftException
exception, we can catch it and give a specific and useful error to the
user. An important thing to note here is that we have put in a
generic Exception handler in too incase there is some other problem, such as with the database - we have put it after our custom exception handler otherwise the generic one will get executed instead, i.e. the order is important!Back
23.02.2006.
thats really good, its good for Exception handling..
Thanks & regards
Vipin Yadav