C Board  

Go Back   C Board > General Programming Boards > C# Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 03-14-2006, 12:55 AM   #1
Registered User
 
Join Date: Oct 2005
Posts: 3
Handling Exceptions

I would like someone to help me understand the process of throwing and handling exceptions based on the following questions. If I create a class pizza that has two fields and a get method for each field that throws a exception relative to each field. Do I have to create a xxxxexception class? If I do is this example close to what the class should look like:

public xxxxexception class

\\declare variables
public string name
public int number

\\constructor
public xxxxexception (string name,int number)

\\try

\\catch

\\finally

I'm really confused
augie0216 is offline   Reply With Quote
Old 03-14-2006, 09:23 AM   #2
Anti-Poster
 
Join Date: Feb 2002
Posts: 1,229
You've got several options. You could certainly create an exception class for each kind of exception you want to throw. You could create an exception class that held an enumeration that you could check to see what was thrown (I wouldn't, though; it's easier to catch derived classes of exceptions). You could create a generic exception class for catching any exception thrown by a specific class. It really depends on what you need to do.

If you're planning on using exceptions, you should decide which of the above best fits your needs. For example, is it more important for you to catch all exceptions generated by one class or to catch one kind of exception generated by any class? It's two different kinds of hierarchy that can't be easily merged.

In general, all you need to do to create an exception class is derive from either Exception or ApplicationException and provide a few constructors. For example:
Code:
using System.Runtime.Serialization;

[Serializable]
public class UserException : ApplicationException
{
	public UserException()
	{

	}

	public UserException(string message)
		: base(message)
	{

	}

	public UserException(string message, Exception innerException)
		: base(message, innerException)
	{

	}

	public UserException(SerializationInfo info, StreamingContext context)
		: base(info, context)
	{

	}
}
If you'd like to catch exceptions based on the class that threw it, you can create one generic exception class to handle all of them. It's exactly the same as the above except for the declaration:
Code:
[Serializable]
public class UserException<T> : UserException
Instead, if you want exceptions based on the action taken to throw it, all you have to do is derive your type of exception from your UserException and create the same old constructors as before:
Code:
[Serializable]
public class MathException : UserException
The reason it's difficult to have it both ways is that C# doesn't allow multiple inheritance. For example, if I wanted to have a generic MathException class, I'd want to derive it from both the generic UserException and the regular MathException classes. Since C# doesn't allow that, you have to choose one or the other.

Attached is a sample code file showing exception classes and handling of exceptions.
Attached Files
File Type: txt Program.txt (3.1 KB, 43 views)
__________________
Rule #1: Every rule has exceptions
pianorain is offline   Reply With Quote
Old 03-14-2006, 11:04 PM   #3
Registered User
 
Join Date: Oct 2005
Posts: 3
Thanks for the help
augie0216 is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
signal handling and exception handling lehe C++ Programming 2 06-15-2009 10:01 PM
event handling is serialized in MS Visual Studio C++ 2005 ?? mynickmynick Windows Programming 3 08-07-2008 04:47 AM
Exception handling framework based on multiple inheritance Mario F. C++ Programming 11 06-25-2007 10:17 AM
Handling Global Project Wide Exceptions doom C# Programming 0 09-12-2005 10:17 AM


All times are GMT -6. The time now is 12:41 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22