c# - How to see which string threw a FormatException? -


i have method parses currency strings (such "€4.00" or "$14.50"), there parsing error, , throws formatexception.

what want do, send string couldn't parsed (threw exception) database.

try {     string euronumber = "€4.00";      // throw formatexception     double parsednumber = double.parse(euronumber, numberstyles.currency); } catch (formatexception ex) {     string stringthatthrewtheexception; // should "€4.00" in case      // [omitted] sending server logic } 

is somehow possible? or should use kind of hack?

thank in advance.

as bugfinder said, can use tryparse:

double parsednumber;  var result = double.tryparse(euronumber, numberstyles.currency, cultureinfo.currentculture, out parsednumber); if (!result) {     // send error } 

another alternative move string outside of scope of try block:

    string euronumber = "€4.00";     try     {         // throw formatexception         double parsednumber = double.parse(euronumber, numberstyles.currency);     }     catch (formatexception ex)     {         // have access euronumber here         // [omitted] sending server logic     } 

Comments

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -