![]() |
| | #1 |
| Super Moderator Join Date: Sep 2001
Posts: 4,746
| Exceptions Code: try
{
...
}
catch(Exception e)
{
...
}
Thanks, Sean. |
| sean is offline | |
| | #2 |
| the hat of redundancy hat 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 | |
| | #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 | |
| | #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) 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 | |
| | #5 |
| the hat of redundancy hat 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 | |
![]() |
| Thread Tools | |
| Display Modes | |
|
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 |