Results 1 to 3 of 3

Thread: Throwing My Own Error

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2008
    Location
    Rep of Ireland
    Posts
    1,380

    Throwing My Own Error

    I guys,

    So continuing with my Tagging application. I have a method that returns true or false depending on if it ran successfully. What I would like to do is throw and exception if it does not.

    My question is what does this do for the user? Does this allow them to do a try catch and use my custom exception in the catch clause?

  2. #2
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: Throwing My Own Error

    If by user you mean someone using your code, from the outside (like we are using the code in the .NET framework), then yes. It's no different then us catching a ArgumentOutOfRangeException, or something like that. Somewhere in the framework source, there's a check if some argument is valid, and if not, the exception is thrown
    Code:
    if (argument >= list.Count)
       throw new ArgumentOutOfRangeException("probably something important here");
    then, outside of this class, a user uses this as
    Code:
    try
    {
       list[3].Name = "Nick";
    }
    catch (ArgumentOutOfRangeException ex)
    {
       MessageBox.Show(ex.Message);
    }
    It's just the same for your own code, just replace "ArgumentOutOfRange" with whatever is appropriate for your exception.


    EDIT
    In case you were wondering how to create your own type of exception, just create a class that inherits Exception and add your custom functionality, if any.

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2008
    Location
    Rep of Ireland
    Posts
    1,380

    Re: Throwing My Own Error

    Cheers, That works!

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