C# - 失败解析异常? [英] C# - failed parse exception?

查看:163
本文介绍了C# - 失败解析异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写在C#程序,我想赶上引起的异常转化(空) 为int。什么是异常的名字?

编辑: 我不知道我在这里可以显示完整的code ... 但我敢肯定,你并不需要完整的code,因此:

  INT NUM1 = Int32.Parse(number1.Text);
        INT NUM2 = Int32.Parse(number2.Text);
 

解决方案

如果你能避免它,不要code。通过例外!

您正在寻找异常名称叫做出现FormatException。

不过,这将是更明智的,首先做的TryParse你正在尝试分析,在对象上,例如

  INT I;
如果(!int.TryParse(我,超时值))
{
    //你抓住它未抛出异常。
}
 

I am writing a program in C#, and I want to catch exceptions caused by converting "" (null) to int. What is the exception's name?

EDIT: I'm not sure i can show here the full code... But i'm sure you don't need the full code, so:

        int num1 = Int32.Parse(number1.Text);
        int num2 = Int32.Parse(number2.Text);

解决方案

If you can avoid it, do not code by exception!

The exception name you are looking for is called a FormatException.

However, it would be smarter to first do a TryParse on the object you are attempting to parse, e.g.

int i;
if(!int.TryParse(i,out value))
{
    // You caught it without throwing an exception.
}

这篇关于C# - 失败解析异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆