C# - failed parse exception?

后端 未结 10 1377
春和景丽
春和景丽 2021-02-19 07:35

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 sur

10条回答
  •  星月不相逢
    2021-02-19 07:53

    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 value;
    if(!int.TryParse("1", out value))
    {
        // You caught it without throwing an exception.
    }
    

提交回复
热议问题