Click here to Skip to main content
15,920,626 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi i am creating function that return List, i want if any exception happen to return exception message with list.
C#
protected List<string> CivilidInfo = new List<string>();
public List<string> GetInformation(String civilid)
{
  try{}
  catch(Exception e)
  {
    CivilidInfo[]=e1.message;  // error
  }
}


how to do it?


[edit]Code block sorted, spurious closing tags removed. - OriginalGriff[/edit]
Posted
Updated 10-Jul-11 0:25am
v4
Comments
Sergey Alexandrovich Kryukov 10-Jul-11 15:59pm    
Why on Earth? It smells exception abuse based on lack of understanding how and why using exceptions. If you explain the purpose (you removed "answer" does not explain it), I'll probably help.
--SA

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?
 
Share this answer
 
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: 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]
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 10-Jul-11 16:02pm    
Griff, there is really no such convention -- just avoid conflict. Why using "e" for event Arguments -- too short name; I never do it?

Returning exception and exception list is a clear abuse of exceptions -- they are designed to avoid returning any exception/error information.
--SA
OriginalGriff 11-Jul-11 2:42am    
Because VS autogenerates event handlers with "e" as the EventArg - and it rarely gets changed! :laugh:
If you look at the MSDN documentation, every even handler I've see has "sender" and "e" as the paramaters. This establishes a convention of sorts, even if unwritten.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900