Exceptions

This is a discussion on Exceptions within the C# Programming forums, part of the General Programming Boards category; (yes, another question from me) Code: try { ... } catch(Exception e) { ... } I noticed that not all ...

  1. #1
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913

    Exceptions

    (yes, another question from me)

    Code:
    try
    {
    	...
    }
    catch(Exception e)
    {
       ...
    }
    I noticed that not all exceptions are derived from the Exception class, and so I was wandering: Would the code above execute the catch code regardless of which sepcific type of exception was thrown? I'd like to be able to give the user some information about what type of exception occured, but if the above is not possible, do I have to specify and argument for the catch block? And then am I able to handle multiple types of exceptions with multiple catch blocks?

    Thanks,
    Sean.

  2. #2
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,139
    Contrary to C++ where you can just catch(...) and throw anything you like in .NET you can only throw Exceptions( and derived classes ) and you can only catch Exceptions and derived classes.

    You can specialize your catch blocks. For example:

    try
    {
    // something
    }
    catch( NotSupportedException nse )
    {
    // something threw a NotSupportedException
    }
    catch( ArgumentNullException ane )
    {
    // something threw an ArgumentNullException
    }
    catch( Exception e )
    {
    // something threw anything but NotSupported or ArgumentNull Exceptions
    }

    You can derive your own exception classes from Exception ( better yet there are already two subclasses you should derive from, look it up in the MSDN, I don't remember the names ) and use it just like the derived exception classes in the .NET Framework.

    public class FooException : Exception
    {

    }

    try
    {
    throw new FooException;
    }
    catch( FooException foo )
    {
    // will be caught
    }

    try
    {
    throw new FooException;
    }
    catch( Exception e )
    {
    // will be caught
    }
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  3. #3
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511
    Sean,

    Why don't you use the Message property it provides a short description of the exception:

    Code:
    try
    {
    
    }
    catch(DivideByZeroException e)
    {
        Console.WriteLine(e.Message);
    }
    Mr. C: Author and Instructor

  4. #4
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    Oh - I know about the Message property - thanks. What I'm asking is if I can use
    Code:
    catch[Exception e)
    and have that code executed in the event that ANY exception occurs. Will this happen? I'm afraid I found nvoight response a little confusing.

    The reason I'm asking is because during testing I may not simulate conditions that a certain user might use that would generate a certain exception. All the common exceptions will be handled specifically, but I just want a kind of catch-all block. Would the above work?

    edit: Grr.... I just realized that a "finally" block would do exactly what I wanted... Well I'll leave the rest here to explain what I was asking and for anyone else who's searching the forums. Thanks for the help guys!
    Last edited by sean; 09-30-2004 at 01:21 PM.

  5. #5
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,139
    Indeed, a catch( Exception e ) will catch anything that is thrown, just like catch( ... ) in C++ does
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. HttpWebResponse Exceptions
    By Boomba in forum C# Programming
    Replies: 1
    Last Post: 01-03-2008, 11:43 AM
  2. Debug --> Exceptions in Visual Studio 2005
    By George2 in forum C# Programming
    Replies: 1
    Last Post: 08-10-2007, 02:12 AM
  3. Exceptions that should terminate. Really.
    By Mario F. in forum C++ Programming
    Replies: 7
    Last Post: 06-26-2007, 10:56 AM
  4. Need advice: catch exceptions or call methods to check bits?
    By registering in forum C++ Programming
    Replies: 1
    Last Post: 10-03-2003, 01:49 PM
  5. Throwing exceptions with constructors
    By nickname_changed in forum C++ Programming
    Replies: 14
    Last Post: 07-08-2003, 09:21 AM

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