C Board  

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

Reply
 
LinkBack Thread Tools Display Modes
Old 09-27-2004, 03:46 PM   #1
Super Moderator
 
Join Date: Sep 2001
Posts: 4,746
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.
sean is offline   Reply With Quote
Old 09-28-2004, 02:31 PM   #2
the hat of redundancy hat
 
nvoigt's Avatar
 
Join Date: Aug 2001
Location: Hannover, Germany
Posts: 2,769
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.
nvoigt is online now   Reply With Quote
Old 09-29-2004, 08:10 PM   #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
Mister C is offline   Reply With Quote
Old 09-30-2004, 01:16 PM   #4
Super Moderator
 
Join Date: Sep 2001
Posts: 4,746
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.
sean is offline   Reply With Quote
Old 10-01-2004, 12:48 AM   #5
the hat of redundancy hat
 
nvoigt's Avatar
 
Join Date: Aug 2001
Location: Hannover, Germany
Posts: 2,769
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.
nvoigt is online now   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 05:52 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