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
  1. Home
  2. General Programming
  3. C#
  4. FormatException

FormatException

Scheduled Pinned Locked Moved C#
questionhelp
5 Posts 3 Posters 0 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.
  • 1 Offline
    1 Offline
    123456uio
    wrote on last edited by
    #1

    Hello I´ve got a string and i would like to convert into an int but the problem is that when i don´t have an int an exception is throw (FormatException). How can i catch this expceion? The try{catch(Exception e) doen´t work. I use System.Convert.ToInt32(string). Thank you in advance, Borja Riesgo Juan

    M 1 Reply Last reply
    0
    • 1 123456uio

      Hello I´ve got a string and i would like to convert into an int but the problem is that when i don´t have an int an exception is throw (FormatException). How can i catch this expceion? The try{catch(Exception e) doen´t work. I use System.Convert.ToInt32(string). Thank you in advance, Borja Riesgo Juan

      M Offline
      M Offline
      Martin 0
      wrote on last edited by
      #2

      Hello, Try catch should work in this case, but is not recommended. Instead you should use the int.Parse Method. All the best, Martin

      G 1 Reply Last reply
      0
      • M Martin 0

        Hello, Try catch should work in this case, but is not recommended. Instead you should use the int.Parse Method. All the best, Martin

        G Offline
        G Offline
        Guffa
        wrote on last edited by
        #3

        Martin# wrote:

        Instead you should use the int.Parse Method.

        I think that you mean the int.TryParse method, as the int.Parse method doesn't offer anything over Convert.ToInt32. For framework 1.x you use double.TryParse with NumberStyles.Integer and then cast the value to int.

        --- b { font-weight: normal; }

        M 1 Reply Last reply
        0
        • G Guffa

          Martin# wrote:

          Instead you should use the int.Parse Method.

          I think that you mean the int.TryParse method, as the int.Parse method doesn't offer anything over Convert.ToInt32. For framework 1.x you use double.TryParse with NumberStyles.Integer and then cast the value to int.

          --- b { font-weight: normal; }

          M Offline
          M Offline
          Martin 0
          wrote on last edited by
          #4

          Hello Guffa, Thank you for your info. I normaly work with double.TryParse(), but I never tried the NumberStyles.Integer. So I guess you would also recommend using double.TryParse plus cast to int, instead of try catch with Convert.ToInt32. So I think I'm going to change some of my code. So once again thanks for your help. All the best, Martin

          G 1 Reply Last reply
          0
          • M Martin 0

            Hello Guffa, Thank you for your info. I normaly work with double.TryParse(), but I never tried the NumberStyles.Integer. So I guess you would also recommend using double.TryParse plus cast to int, instead of try catch with Convert.ToInt32. So I think I'm going to change some of my code. So once again thanks for your help. All the best, Martin

            G Offline
            G Offline
            Guffa
            wrote on last edited by
            #5

            Martin# wrote:

            So I guess you would also recommend using double.TryParse plus cast to int, instead of try catch with Convert.ToInt32.

            Yes. Exceptions are mainly for unexpected situations. If you expect something to happen, you should try to stop it before it happens. Another alternative is to use a regular expression to validate the string before you convert it. If you have verified that a string only contains digits and not more than fits in the range of an int, you can safely convert it to an int.

            --- b { font-weight: normal; }

            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