Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Exceptions

Exceptions

Scheduled Pinned Locked Moved C#
8 Posts 6 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Jim Warburton
    wrote on last edited by
    #1

    I have defined a user Exception class. The exception will be thrown in 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

    this thing looks like it was written by an epileptic ferret Dave Kreskowiak

    G J C J P 5 Replies Last reply
    0
    • J Jim Warburton

      I have defined a user Exception class. The exception will be thrown in 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

      this thing looks like it was written by an epileptic ferret Dave Kreskowiak

      G Offline
      G Offline
      Giorgi Dalakishvili
      wrote on last edited by
      #2

      If I were you, I would use Int.TryParse to check if the input string represents a number

      #region signature my articles #endregion

      1 Reply Last reply
      0
      • J Jim Warburton

        I have defined a user Exception class. The exception will be thrown in 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

        this thing looks like it was written by an epileptic ferret Dave Kreskowiak

        J Offline
        J Offline
        Judah Gabriel Himango
        wrote on last edited by
        #3

        public int GetNextInt()
        {
            int parsedValue;
            bool success = int.TryParse(GetNextToken(), out parsedValue);
            if(!success)
            {
                throw MySuperSpecialException(...);
            }

        return parsedValue;
        }

        Tech, life, family, faith: Give me a visit. I'm currently blogging about: Forgive Me Father, For I Have Sinned: A Catholic Confession Regarding Sabbath The apostle Paul, modernly speaking: Epistles of Paul Judah Himango

        A 1 Reply Last reply
        0
        • J Jim Warburton

          I have defined a user Exception class. The exception will be thrown in 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

          this thing looks like it was written by an epileptic ferret Dave Kreskowiak

          C Offline
          C Offline
          codemunch
          wrote on last edited by
          #4

          public static bool TryParse(string s, out int result); Int32 myint; if(!Int32.TryParse(",", out myint)) throw new YourCustomException(); return myint;

          1 Reply Last reply
          0
          • J Jim Warburton

            I have defined a user Exception class. The exception will be thrown in 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

            this thing looks like it was written by an epileptic ferret Dave Kreskowiak

            J Offline
            J Offline
            Jim Warburton
            wrote on last edited by
            #5

            The answer is to use Int32.TryParse():doh:. Modified Thanks to all for your answers.

            this thing looks like it was written by an epileptic ferret Dave Kreskowiak

            1 Reply Last reply
            0
            • J Judah Gabriel Himango

              public int GetNextInt()
              {
                  int parsedValue;
                  bool success = int.TryParse(GetNextToken(), out parsedValue);
                  if(!success)
                  {
                      throw MySuperSpecialException(...);
                  }

              return parsedValue;
              }

              Tech, life, family, faith: Give me a visit. I'm currently blogging about: Forgive Me Father, For I Have Sinned: A Catholic Confession Regarding Sabbath The apostle Paul, modernly speaking: Epistles of Paul Judah Himango

              A Offline
              A Offline
              Anthony Mushrow
              wrote on last edited by
              #6

              A SUPER special exception? Must be some important error to get that kind of treatment.

              My current favourite word is: PIE! Good ol' pie, it's been a while.

              C 1 Reply Last reply
              0
              • A Anthony Mushrow

                A SUPER special exception? Must be some important error to get that kind of treatment.

                My current favourite word is: PIE! Good ol' pie, it's been a while.

                C Offline
                C Offline
                codemunch
                wrote on last edited by
                #7

                The original OP wanted to throw their own custom exception...Personally, I prefer to call them all "ABone" and let future maintenance developers sort it out :D EDIT: Or even "AFrikinBone" if you like sharks with lasers...

                1 Reply Last reply
                0
                • J Jim Warburton

                  I have defined a user Exception class. The exception will be thrown in 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

                  this thing looks like it was written by an epileptic ferret Dave Kreskowiak

                  P Offline
                  P Offline
                  PIEBALDconsult
                  wrote on last edited by
                  #8

                  If you need to know what the actual exception was...

                  public int GetNextInt()
                  {
                  try
                  {
                  return System.Int32.Parse(GetNextToken());
                  }
                  catch ( System.Exception err )
                  {
                  throw new MyException ( "blah blah blah" , err ) ;
                  }
                  }

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups