|
-
April 22nd, 2008, 07:27 PM
#1
Simple throw question...
Since I upgraded to Visual Studio 2008 Express Edition, my help seem totaly screwed up.... What happened to the simple and very helpful help that was in 2005???? Anyways.. my question...
I simple want to throw an exception if some event is true...
how do I code the try catch for something like..
if (x < 0)
throw("Parameter out of bounds.");
-
April 22nd, 2008, 07:47 PM
#2
Re: Simple throw question...
Here is a good explanation about it.
As far as the VC++ help, you can also use msdn.
-
April 22nd, 2008, 10:37 PM
#3
Re: Simple throw question...
Did I post to the wrong group... This is C# isn't it?
-
April 23rd, 2008, 07:30 AM
#4
Re: Simple throw question...
 Originally Posted by JustSomeGuy
how do I code the try catch for something like..
Code:
if (x < 0)
throw("Parameter out of bounds.");
Code:
try
{
if (x < 0)
throw new Exception("Parameter out of bounds.");
}
catch
{
Console.WriteLine("");
}
-
April 23rd, 2008, 08:50 AM
#5
Re: Simple throw question...
Except throw an 'ArgumentOutOfRange' exception as opposed to an 'Exception'.
i.e.
throw new ArgumentOutOfRangeException ("x");
www.monotorrent.com For all your .NET bittorrent needs
NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.
-
April 23rd, 2008, 08:59 AM
#6
Re: Simple throw question...
 Originally Posted by Mutant_Fruit
Except throw an 'ArgumentOutOfRange' exception as opposed to an 'Exception'.
i.e.
throw new ArgumentOutOfRangeException ("x");
Be nice 
... You're right though
It's not a bug, it's a feature!
-
April 23rd, 2008, 09:14 AM
#7
Re: Simple throw question...
-
April 23rd, 2008, 09:46 AM
#8
Re: Simple throw question...
 Originally Posted by JustSomeGuy
Did I post to the wrong group... This is C# isn't it?
Woops, sorry my bet.
-
April 23rd, 2008, 10:26 AM
#9
Re: Simple throw question...
 Originally Posted by opedog
Code:
try
{
if (x < 0)
throw new Exception("Parameter out of bounds.");
}
catch
{
Console.WriteLine("");
}
True enough, that ParameterOutOfBounds is probably the write exception to throw... However, in you example, how would you get the Message that was thrown with the execption... Is it in the Exception class?
Thanks!
-
April 23rd, 2008, 10:49 AM
#10
Re: Simple throw question...
 Originally Posted by JustSomeGuy
True enough, that ParameterOutOfBounds is probably the write exception to throw... However, in you example, how would you get the Message that was thrown with the execption... Is it in the Exception class?
Thanks!
Replace the catch bit of my example with this:
Code:
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
This is also permissable:
Code:
catch (ArgumentOutOfBoundsException aoobex)
{
Console.WriteLine(aoobex.ToString());
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|