How to do proper exception handling and return codes for Console Application
I have a simple C# console application that talks to a webservice. I am
not sure if the way I am handling expections and returning error codes are
proper. Would like to have comments on good pratice to do this. (I am
using CommandLine.dll for option parsing)
class Program
{
static int Main(string[] args) {
var options = new Options();
if (CommandLine.Parser.Default.ParseArguments(args, options)) {
try {
var client = new MyWebService();
var response = client.MyFunction(new MyRequest() {
Param1 = options.param1, Param2 = options.Param2 });
if (response.ErrorCode != 0) {
Console.WriteLine("Error code= " +
response.ErrorCode);
}
else {
File.WriteAllText(options.OutputFile, response.File);
return 0;
}
}
catch (Exception exp) {
Console.WriteLine(exp.Message);
return 1;
}
}
return 1;
}
}
No comments:
Post a Comment