本文介绍了C#我怎么检查异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
例如在控制台中我必须输入3个数据`
For example in console i must input 3 datas`
string,string,int
。
如果我写错了任何一个字,示例第一个数据我写为整数,它会警告我任何消息,例如`
.
if I write any of the words wrongly,for example first data i wrote as integer,it will warn me with any message,for example`
(some data are not correct)
我怎么能通过try catch写出Exception,这个条件我必须在try catch中写出来吗?
我尝试了什么:
也许我必须用int.TryParse写?
How can i write that Exception through try catch,which condition i must write in try catch?
What I have tried:
Maybe i must write with int.TryParse?
推荐答案
For int you must try TryParse method.
For string u can typecast like :
int intA = 10;
string a = (string) intA;
but sometimes this may throw exceptions.
Instead try using :
int intA = 10;
string a = Convert.ToString(intA);
The Convert.ToString() method will never throw exception and handles null as well when compared to typecasting.
这篇关于C#我怎么检查异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!