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(strin g filepath)
{
try
{
// Do dangerous stuff.
}
catch (System.Excepti on exc)
{
if (exc is MyHandledExcept ion)
{
// Do intelligent stuff.
}
else
{
return 1;
throw exc;
}
}
}
//------------------------------------------------------------
Is the final part even ok? Do I need the Finally statement for this?
Your thoughts. 2 2573
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.Exceptio n exc , and determine the type
again, you really should do something like
try
{
}
catch(MyHandled Exception myHandleExcepti on)
{
}
catch(MyOtherEx cetion 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(strin g filepath)
{
try
{
// Do dangerous stuff.
}
catch (System.Excepti on exc)
{
if (exc is MyHandledExcept ion)
{
// Do intelligent stuff.
}
else
{
return 1;
throw exc;
}
}
}
//------------------------------------------------------------
Is the final part even ok? Do I need the Finally statement for this?
Your thoughts.
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 programmer that you pretend to be, why don't you
just write some? (And oh, void main is still not allow by the C++ standard.)
Seeming as how you tried to quote the standard in an attempt to pretend
you're right, I'll quote the standard to once...
|
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 minutes later.
may i use the static int Main return value?
if that was the solution, so how do i provide that value?
i have invoked the app from dos cmd, but nothing has been returned, or
at least printed on the screen.
|
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 logic layer) that inserts a user into a
database, and returns the newly created UserID:
public function CreateUser(userName as string, userEmail as string)
dim userID as integer
|
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 it needs to return an exit code
to that script.
I've tried a few ways to return an exit code.
1. As a forms app with Entry point being Form1, I've tried
Application.Exit(999). It does not return the exit code.
|
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 app.
My problem is that I need to react to the ERRORLEVEL that is returned by
the console executables in each case.
| |
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 be able to return a
meaningful value (e.g. Divide by zero) and null (or other default) is
returned instead. This is not a fatal error, so processing can
continue on the rest of the data table. As such, the table may
ultimately end up with a...
|
by: junky_fellow |
last post by:
Guys,
Can I return 0, from main() ? Is this equivalent to
exit(EXIT_SUCCESS) ?
thanks for any help...
|
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 method.
QUOTE ENDS HERE
I have never heard anyone else say that it is a problem for a function
|
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 syntax" are the only valid ones.
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |