C#返回值和错误处理

时间:2016-10-06 08:18:35

标签: c# error-handling return

是否可以在方法中返回List或int?

类似的东西:

  • 如果成功:返回列表
  • 如果失败:返回int(给出错误号)或带错误消息的字符串。

(失败,因为没有例外,但值不正确,如PointF.X =低于0或最低值超过10)。

现在我这样做:

    public List<PointF> GetContourInfoFromFile(string a_file)
    {
        ListContourPoints.Clear();
        List<string> textLines = new List<string>();

        bool inEntitiesPart = false;
        bool inContourPart = false;

        try
        {
            foreach (string str in File.ReadAllLines(a_file))
            {
                textLines.Add(str);//Set complete file in array
            }

            for (int i = 0; i < textLines.Count; i++)
            {
              //read complete file and get information and set them in ListContourPoints
            }

            //Check Coordinate values
            for (int i = 0; i < ListContourPoints.Count; i++)
            {
                //Coordinates are below -1!
                if (ListContourPoints[i].X < -1 || ListContourPoints[i].Y < -1)
                {
                    ListContourPoints.Clear();
                    break;
                }
                //Lowest X coordinate is not below 10!
                if (mostLowestXContour(ListContourPoints) > 10)
                {
                    ListContourPoints.Clear();
                    break;
                }
                //Lowest Y coordinate is not below 10!
                if (mostLowestYContour(ListContourPoints) > 10)
                {
                    ListContourPoints.Clear();
                    break;
                }
            }
        }
        catch (Exception E)
        {
            string Error = E.Message;
            ListContourPoints.Clear();
        }
        return ListContourPoints;
    }

当我这样做的时候,我知道te值存在问题......但不是具体是什么。

那我怎么解决这个问题呢?如果返回列表OR string / int是不可能的,那么最佳解决方案是什么?

4 个答案:

答案 0 :(得分:3)

你可以

解决方案1:

如果erreur抛出异常,并在上面的代码中执行try catch

删除函数中的part catch,当你调用函数时,在try catch中执行以下操作:

 try{
     List<PointF> result=GetContourInfoFromFile(youfile);
    }
    catch (Exception E)
    {
        string Error = E.Message;
    }

解决方案2:

使用Listresult返回一个对象,并将错误作为属性

答案 1 :(得分:2)

您可以从catch块中抛出异常,而不是返回一个数字,以便外部异常处理程序可以捕获它。

以下是一个示例:

    public void MainMethod()
    {
        try
        {
            var myList = SomeMethod();
        }
        catch(Exception ex)
        {
            Console.WriteLine(ex.Message); // prints out "SomeMethod failed."
        }
    }

    public List<object> SomeMethod()
    {
        try
        {
            int i = 1 + 1;

            // Some process that may throw an exception

            List<object> list = new List<object>();
            list.Add(1);
            list.Add(i);

            return list;
        }
        catch(Exception ex)
        {
            Exception newEx = new Exception("SomeMethod failed.");
            throw newEx;
        }
    }

答案 2 :(得分:1)

一种解决方案是返回对象

  public object GetContourInfoFromFile(string a_file)
  {

  }

并且在你调用它的方法中尝试强制转换为int和list,看看哪一个成功。

更精细的解决方案是拥有一个类

public class YourClassName{
   public List<PointF> YourList {get; set;} //for success
   public int YourVariable {get; set} // for failure
   public string YourMEssage {get; set} // for failure
}

并返回那个

  public YourClassName GetContourInfoFromFile(string a_file)
  {

  }

答案 3 :(得分:0)

你可以创建一个包含值或错误代码的包装类。例如:

public class Holder<T>
{
    private Holder(T value)
    {
        WasSuccessful = true;
        Value = value;
    }

    private Holder(int errorCode)
    {
        WasSuccessful = false;
        ErrorCode = errorCode;
    }

    public bool WasSuccessful { get; }
    public T Value { get; }
    public int ErrorCode { get; }

    public static Holder<T> Success(T value)
    {
        return new Holder<T>(value);
    }

    public static Holder<T> Fail(int errorCode)
    {
        return new Holder<T>(errorCode);
    }
}

用法:

public Holder<PointF> MyFunc()
{
    try
    {
        //
        return Holder<PointF>.Success(new PointF());
    }
    catch
    {
        return Holder<PointF>.Fail(101);
    }
}