Handling Exceptions

This is a discussion on Handling Exceptions within the C# Programming forums, part of the General Programming Boards category; I would like someone to help me understand the process of throwing and handling exceptions based on the following questions. ...

  1. #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

  2. #2
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    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 Attached Files
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    3
    Thanks for the help

Popular pages Recent additions subscribe to a feed

Similar Threads

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

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