2014年7月13日 星期日

[RESOLVED] Why exception cannot be raised


Hi,

why can't I detect it as an exception, when tb_ite.Text is having "b" inside?


int int1;
try
{
int.TryParse(tb_ite.Text, out int1);
}
catch (Exception ex)
{
...
}
finally
{
}







Becoz u r tyring to parse here, it would have thrown exception if u used
Int32.Parse
instead.


The TryParse method is like the
Parse method, except the
TryParse method does not throw an exception if the conversion fails. It eliminates the need to use exception handling to test for a
FormatException in the event that
s is invalid and cannot be successfully parsed.



tryparse is cobination Try and Parse


If any exception will return false


this to avoid exception message.


Whatever the result you will not get exception.



Hi,


Just see the documentation :
http://msdn.microsoft.com/en-us/library/system.int32.tryparse.aspx


TryParse doesn't though an exception. Instead it return a boolean value that tells if the conversion is value so you could check the return value for the TryParse function rather than having a try/catch construct.


沒有留言:

張貼留言