473,406 Members | 2,439 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

main returning errorlevel & exceptions

Here's the situation. My program will be able to start with an argument
(a path to a file) and then run a batch of commands in that file. So if
an argument is provided to the main method, the program logic moves to
the classes/methods that execute the batch (instead of s normal, GUI
driven program execution).

Should an error occur somewhere during the batch, I really need to
catch it, so that I can return my main method with an errorlevel. But
at the same time, I would like to rethrow any exception I cannot
handle. So here's the problematic code:

//------------------------------------------------------------
public int Main(string[] args)
{
return RunScript(args[0]);
}
//------------------------------------------------------------
public int RunScript(string filepath)
{
try
{
// Do dangerous stuff.
}
catch (System.Exception exc)
{
if (exc is MyHandledException)
{
// Do intelligent stuff.
}
else
{
return 1;
throw exc;
}
}
}
//------------------------------------------------------------

Is the final part even ok? Do I need the Finally statement for this?

Your thoughts.

Oct 31 '06 #1
2 2560
A couple of suggestions :

1return 1;
throw exc;

I don't think the throw exc will ever be executed, it's unreachable code.

2You shouldn't catch System.Exception exc , and determine the type
again, you really should do something like

try
{
}
catch(MyHandledException myHandleException)
{
}
catch(MyOtherExcetion otherException)
{
}
catch(Exception )
{
rethrow here..
}

3There are 2 ways to error handling, returning value and exception.
The suggested way is not to mix them..

By setting and checking some properties on the exception object, you
should avoid using the return value.

Jeroen wrote:
Here's the situation. My program will be able to start with an argument
(a path to a file) and then run a batch of commands in that file. So if
an argument is provided to the main method, the program logic moves to
the classes/methods that execute the batch (instead of s normal, GUI
driven program execution).

Should an error occur somewhere during the batch, I really need to
catch it, so that I can return my main method with an errorlevel. But
at the same time, I would like to rethrow any exception I cannot
handle. So here's the problematic code:

//------------------------------------------------------------
public int Main(string[] args)
{
return RunScript(args[0]);
}
//------------------------------------------------------------
public int RunScript(string filepath)
{
try
{
// Do dangerous stuff.
}
catch (System.Exception exc)
{
if (exc is MyHandledException)
{
// Do intelligent stuff.
}
else
{
return 1;
throw exc;
}
}
}
//------------------------------------------------------------

Is the final part even ok? Do I need the Finally statement for this?

Your thoughts.
Oct 31 '06 #2
Thanks for the reaction. I think I will (A) handle whichever exception
i can with a messagebox followed by returning the Main method with an
errorlevel and (B) rethrow any unhandled exception.

Oct 31 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

192
by: Kwan Ting | last post by:
The_Sage, I see you've gotten yourself a twin asking for program in comp.lang.c++ . http://groups.google.co.uk/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&th=45cd1b289c71c33c&rnum=1 If you the oh so mighty...
1
by: Dan | last post by:
hi ng, i want to give the invoker of my application information whether application has passed successfully. it shall get scheduled, and if the exeution did not pass, it should be repeated some...
3
by: Sean Tynan | last post by:
I want to find out the best way for a method to notify calling code of situations such as validation errors, etc. that may occur during method execution. e.g. say I have a method (business...
8
by: Zeno Lee | last post by:
What is the best way to return an exit code from a VB.NET windows forms app? My Forms application is dual purpose. It is an interactive windows app. It is also automated and run via a script and...
5
by: Luke Vogel | last post by:
Hi all ... I'm writing a small application that needs to run a number of console executables. No problem, I can use the 'shell' function with all its useful parameters to run each console...
3
by: roger.dunham | last post by:
Hi there, I am writing an application that performs calculations on records within a data table. There may be many records in a data table. There are situations where the calculation may not...
27
by: junky_fellow | last post by:
Guys, Can I return 0, from main() ? Is this equivalent to exit(EXIT_SUCCESS) ? thanks for any help...
23
by: pauldepstein | last post by:
Below is posted from a link for Stanford students in computer science. QUOTE BEGINS HERE Because of the risk of misuse, some experts recommend never returning a reference from a function or...
80
by: Ioannis Vranos | last post by:
Hi, in C90 is "int main()" valid, the same as "int main(void)" and "int main(int argc, char *argv)"? AFAIK in C99 only "int main(void)" and "int main(int argc, char *argv) - and the **argv...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.