| | What happens if I assigned a string to an integer? Because I used a try statement for it and the catch didn't get invoked! Does it just assign a to 0!? |
|
|
QUOTE(8bit @ Feb 19 2005, 08:41 AM) What happens if I assigned a string to an integer? Because I used a try statement for it and the catch didn't get invoked! Does it just assign a to 0!? I suppose you're using Java. You can easily assign a String to an Integer by using parseInt. String s = "777"; int x = Integer.parseInt(s); QUOTE I suppose you're using Java. Ok, let's think about this. This is the C/C++ programming forum. What 8bit just said makes perfect sense in C++ (exception handling), so it's probably not Java You can easily assign a String to an Integer by using parseInt. String s = "777"; int x = Integer.parseInt(s); QUOTE What happens if I assigned a string to an integer? Because I used a try statement for it and the catch didn't get invoked! Does it just assign a to 0!? Could you please state what compiler you're using (version also) and perhaps the source code? Normally that would not even compile with what I'm using.
I can't see it being an accidental thing, as if it were, without casting it, the compiler would complain.
So, if you did cast it... like int x = (int)"777"; then it wouldn't be caught because it wouldn't fail. It wouldn't be correct, but it definately wouldn't fail... I'm still trying to fathom the need for crossing types in this case... if you want to assign a the value in a string to an int, you'd just use atoi(). I'm really interested to see why this is necessary. More details would be nice.
I used MingW and wrote the source with: CONText...but I might be able to find out the problem. And uh...leeiah this is a C/C++ board...why would I post Java stuff here? Very hard question, eh?
I mean source code as in what you wrote...
MingW eh? I only use plain old gcc, so I can't help you with that one. QUOTE Very hard question, eh? Well yes and no, see, it's not defined in the standard as to whether or not you can do so. Why don't you compile it and give it a try? We're all stumped. |
|
![]() ..what Happens If |
| ADD REPLY / Got an Opinion! | Remove these ADs! | RAPID SEARCH! | Free Web Hosting | [X] |
|
Express your Opinions, Thoughts or Contribute more info. to help others. Ask your Doubts & Queries to get answers, So that "Together We can help others!" |
Register FREE for AD-FREE forum, Create your own topics, Ask Questions, track topics, setup subscriptions & notifications and Get a Free Website w/ Email and FTP. | 500MB Space *No Ads*, CPanel, FTP, PHP, MySQL, EMails - 100% FREE |
|