在.NET中的ArgumentException

在C#中,当提供给方法的参数无效时,将抛出ArgumentException。

如何处理.NET中的ArgumentException示例

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace application

{

    class Program

    {

        static int Ma(string argument)

        {

          //Handle invalid argument.

            if (argument.Length == 0)

            {

                throw new ArgumentException("Zero-length string invalid", "argument");

            }

            return argument.Length;

        }

        static void Main()

        {

             //using try and catch block

            try

            {

                Ma("");

            }

            catch (Exception ex)

            {

                Console.WriteLine( ex);

                Console.WriteLine("HelpLink = {0}", ex.HelpLink);

                Console.WriteLine("Message = {0}", ex.Message);

                Console.WriteLine("Source = {0}", ex.Source);

                Console.WriteLine("StackTrace = {0}", ex.StackTrace);

            }

            Console.ReadLine();

        }       

    }

}

ArgumentNulLException和ArgumentoutOfRangeException是来自ArgumentException的派生类。

日期:2020-04-11 22:50:13 来源:oir作者:oir