我的需求是要求对一个数组进行操作,操作前需对参数数组进行判定,如果该数组长度为1或者所有数字都一样时则抛出一个自定义的异常,请各位高手指点迷津!!!(不考虑用程序逻辑判断)
用C#麻烦大侠写下给我参考下!非常感谢!

解决方案 »

  1.   

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Runtime.Serialization;namespace HKH.Exception
    {
    [Serializable]
    public class HKHException : ApplicationException
    {
    /// <summary>
    /// do not allow creation of exception with no message
    /// </summary>
    private HKHException()
    {
    } /// <summary>
    /// Constructor takes problem message to be thrown
    /// invokes constructor on ApplicationException
    /// </summary>
    public HKHException(string message)
    :base(message)
    {
    } /// <summary>
    /// Constructor takes problem message and caught exception
    /// invokes constructor on ApplicationException
    /// </summary>
    public HKHException(string message, System.Exception ex)
    :base(message, ex)
    {
    } protected HKHException(SerializationInfo si, StreamingContext sc)
    : base(si, sc)
    {
    }
    }}如果消息固定,可以考虑去掉部分构造
      

  2.   

    1、创建自定义异常的类public class ArrayException : ApplicationException
    {
        public ArrayException(string message):base(message)
        {    }
    }2、判断符合条件后,抛出自定义异常protected void Page_Load(object sender, EventArgs e)
        {
            int[] test = { 1, 1, 1, 2 };
            if (test.Length == 1 || test.Distinct().Count() == 1)
                throw new ArrayException("数据长度不能为1,或数据内数据都相同");
            Response.Write("success");
        }