Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Fixing Format Exceptions without using Try Catch

Status
Not open for further replies.

ASPNETnewbie

Programmer
May 9, 2006
93
US
Does anyone know How best to deal with a FormatException Error without using a try catch Statement?

AspNETNewbie
 
If it is truely an error then you need to catch it in the catch block, however you should never control flow using Try/Catch statements. Is the error something that you expect to happen?

I'm not too sure what causes a FormatException error. Could you post some code and give an example of when the error would be thrown?
 
Sample of code causing error

Code:
this.State.Items.Add(new ListItem(state.Ab,state.ID.ToString()));
where State is an enumerated type
 
I don't know what state.Ab is, but you may try adding a .ToString on the end of that as well.
 
State.AB is already a string too. What I have though is this
Code:
this.State.Items.Add(new ListItem(state.Ab,state.ID.ToString("d")));


I added the "d" for formating the information... is that right? I mean since the ID is an integer


ASPNETNEWBIE
 
The parameters to the New ListItem are strings so the formatting won't matter. Trace through in the debugger and see what values actually exist for state.Ab AND state.ID.ToString(). Probably not text as you expect, but a enum that needs to be converted.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top