CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Nov 2006
    Posts
    103

    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.");

  2. #2
    Join Date
    May 2007
    Posts
    811

    Re: Simple throw question...

    Here is a good explanation about it.

    As far as the VC++ help, you can also use msdn.

  3. #3
    Join Date
    Nov 2006
    Posts
    103

    Re: Simple throw question...

    Did I post to the wrong group... This is C# isn't it?

  4. #4
    Join Date
    Mar 2008
    Location
    Atlanta, GA
    Posts
    49

    Re: Simple throw question...

    Quote 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("");
    }

  5. #5
    Join Date
    May 2007
    Posts
    1,546

    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.

  6. #6
    Join Date
    May 2007
    Location
    Denmark
    Posts
    623

    Re: Simple throw question...

    Quote 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!

  7. #7
    Join Date
    Mar 2008
    Location
    Atlanta, GA
    Posts
    49

    Re: Simple throw question...

    Yeah, yeah...

  8. #8
    Join Date
    May 2007
    Posts
    811

    Re: Simple throw question...

    Quote Originally Posted by JustSomeGuy
    Did I post to the wrong group... This is C# isn't it?
    Woops, sorry my bet.

  9. #9
    Join Date
    Nov 2006
    Posts
    103

    Re: Simple throw question...

    Quote 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!

  10. #10
    Join Date
    Mar 2008
    Location
    Atlanta, GA
    Posts
    49

    Re: Simple throw question...

    Quote 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
  •  





Click Here to Expand Forum to Full Width

Featured