C# Exceptions

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jimwawar
    New Member
    • Jan 2007
    • 15

    #1

    C# Exceptions

    I have defined a user Exception class. The exception will be thrown in

    Code:
    public int GetNextInt()
    {
         return Int32.Parse(GetNextToken());
    }
    GetNextToken() reads a file stream, pulling characters until a predefined delimiters are met (space, end of line, etc) returning the string. I can not change anything in GetNextToken.

    If GetNextToken does not return an int value I would like to throw my user defined exception. I have been unable to do so. As soon as as Int32.Parse() finds the input string is not an int it throws an exception.

    Is there a way I can throw my user defined exception or use the data field in the predefined exception to add information.

    Thanks
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    Try something like this:

    Code:
    public int GetNextInt()
    {
       Int32 dummyval=0;
       bool valid=Int32.TryParse(GetNextToken(),out dummyval);
       if(!valid)
       {
          throw new MyCustomException();
       }
       return dummyval;
    }
    OR (maybe)

    Code:
    public int GetNextInt()
    {
       try
       {
          return Int32.Parse(GetNextToken());
       }
       catch(Exception ee)
       {
          throw new MyCustomException();
       }
    }

    Comment

    • jimwawar
      New Member
      • Jan 2007
      • 15

      #3
      Originally posted by Plater
      Try something like this:

      Code:
      public int GetNextInt()
      {
         Int32 dummyval=0;
         bool valid=Int32.TryParse(GetNextToken(),out dummyval);
         if(!valid)
         {
            throw new MyCustomException();
         }
         return dummyval;
      }
      OR (maybe)

      Code:
      public int GetNextInt()
      {
         try
         {
            return Int32.Parse(GetNextToken());
         }
         catch(Exception ee)
         {
            throw new MyCustomException();
         }
      }

      Now I am embarrassed, missed the TryParse completely. Works great, thanks for the reply.

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by jimwawar
        I have defined a user Exception class. The exception will be thrown in

        Code:
        public int GetNextInt()
        {
             return Int32.Parse(GetNextToken());
        }
        GetNextToken() reads a file stream, pulling characters until a predefined delimiters are met (space, end of line, etc) returning the string. I can not change anything in GetNextToken.

        If GetNextToken does not return an int value I would like to throw my user defined exception. I have been unable to do so. As soon as as Int32.Parse() finds the input string is not an int it throws an exception.

        Is there a way I can throw my user defined exception or use the data field in the predefined exception to add information.

        Thanks
        You can also catch whatever exception is being thrown and rethrow your exception in the catch block but (in this case) that is dirtier.

        Comment

        Working...