返回带有列表的异常消息 [英] Return Exception Message with List

查看:65
本文介绍了返回带有列表的异常消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨 我正在创建返回List的函数,我想如果发生任何异常,则返回带有list的异常消息.

Hi i am creating function that return List, i want if any exception happen to return exception message with list.

protected List<string> CivilidInfo = new List<string>();
public List<string> GetInformation(String civilid)
{
  try{}
  catch(Exception e)
  {
    CivilidInfo[]=e1.message;  // error
  }
}



怎么做?


[edit]代码块已排序,伪造的结束标签已删除. -OriginalGriff [/edit]



how to do it?


[edit]Code block sorted, spurious closing tags removed. - OriginalGriff[/edit]

推荐答案

从Exception继承并在其中添加一个列表...但是在我看来,您想使用List实现某些目标作为...我希望您不介意,如果我问,那应该是什么?
Inherit from Exception and put a list in it... but it seems to me, that you want to achieve something with your List as... I hope you won''t mind, if I ask, what that should be?


您可以将异常添加到catch块的列表中.但是无论如何我都不一定要使用外部类级别列表作为返回-如果您返回自己的列表,则更加灵活:


You could add the exception to the list in your catch block. But I wouldn''t necessarily use the external class level list as the return anyway - more flexible if you return your own list:


protected List<string> CivilidInfo = new List<string>();
public List<string> GetInformation(String civilid)
{
  List<string>returnList = new List<string>();
  try{}
  catch(Exception ex)
  {
    returnList.Add(ex.message);  
  }
  return returnList;
}

BTW:惯例是对异常使用"ex"-否则它可能与事件处理程序中的EventArg参数"e"相冲突-只是意味着您可以根据需要更轻松地剪切"n"粘贴. br/>
[edit]忘记了"e"和"ex"位...-OriginalGriff [/edit]

BTW: the convention is to use "ex" for exceptions - otherwise it can conflict with the EventArg parameter "e" in event handlers - just means you can cut''n''paste more easily if you need to.

[edit]Forgot the "e" vs "ex" bit... - OriginalGriff[/edit]


这篇关于返回带有列表的异常消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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