472,954 Members | 1,719 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

clean-up code before throwing an exception may also throw

Hello, in my program I have a function (pseudo code):

void start_mysql_service()
{
obtain handle

start mysql service using handle

if start fails close handle and throw an exception containing error
description

else just close handle and return
}

So no matter if the service is successfully started or not, the handle needs
to be closed to prevent leakage. But the function that closes the handle may
also throw. How should I handle that? catch the close-handle-exception in
start_mysql_service()? Right now I don't catch anything in the
start_mysql_service() function so if it fails to start the service it
prepares to throw an exception, that exception never gets thrown if it the
close handle functions throws. I don't want one error hiding another one.
The real code looks like this and it's ugly (and contains some platform
specific material, sorry about that):
void
start_mysql_service()
{
/* May throw an exception. */
SC_HANDLE mysql_service = get_mysql_service_handle();

if(!StartService(mysql_service, 0, NULL))
{
/* May throw an exception and if it does I never get to throw */
/* the exception indicating that StartService() failed. */
close_service_handle(mysql_service);

/* TODO: Obtain more precise cause of error if possible using
GetLastError() and FormatMessage(). */
throw runtime_error("StartService() failed.");
}

/* May throw an exception. */
close_service_handle(mysql_service);
}
Any ideas how to restructure this into nicer-looking code and solving the
problem of one error hiding another one?

/ E
Jul 22 '05 #1
4 1942
Eric Lilja wrote:
Hello, in my program I have a function (pseudo code):

void start_mysql_service()
{
obtain handle

start mysql service using handle

if start fails close handle and throw an exception containing error
description

else just close handle and return
}
So no matter if the service is successfully started or not, the handle needs
to be closed to prevent leakage. But the function that closes the handle may
also throw. How should I handle that? catch the close-handle-exception in
start_mysql_service()? Right now I don't catch anything in the
start_mysql_service() function so if it fails to start the service it
prepares to throw an exception, that exception never gets thrown if it the
close handle functions throws. I don't want one error hiding another one.
The real code looks like this and it's ugly (and contains some platform
specific material, sorry about that):
Use nested try catch blocks
void
start_mysql_service()
{
/* May throw an exception. */
SC_HANDLE mysql_service = get_mysql_service_handle();

if(!StartService(mysql_service, 0, NULL))
{
/* May throw an exception and if it does I never get to throw */
/* the exception indicating that StartService() failed. */ try
{ close_service_handle(mysql_service); }
catch (...)
{
// we don't care about this exception, we want
// to say what that StartService failed, so we
// catch and ignore it.
} // note: this TODO should probably go before the call to
// call to close_service_handle(), so that any error in c_s_h()
// doesn't mask the results from StartService() /* TODO: Obtain more precise cause of error if possible using
GetLastError() and FormatMessage(). */
throw runtime_error("StartService() failed.");
}

/* May throw an exception. */
close_service_handle(mysql_service);
}
Any ideas how to restructure this into nicer-looking code and solving the
problem of one error hiding another one?

/ E


Jul 22 '05 #2

"Eric Lilja" <er*************@yahoo.com> skrev i en meddelelse
news:cr**********@news.island.liu.se...
Hello, in my program I have a function (pseudo code):

void start_mysql_service()
{
obtain handle

start mysql service using handle

if start fails close handle and throw an exception containing error
description

else just close handle and return
}
First off, the handle should be wrapped into a class providing RAII.

Now your code is reduced into this simpler:

class raii_handle
{
public:
//constructor: obtains handle
//destructor: releases handle
};

void start_mysql_service()
{
handle_class hc(); // constructor allocates

start mysql service using handle
}


So no matter if the service is successfully started or not, the handle
needs to be closed to prevent leakage. But the function that closes the
handle may also throw. How should I handle that? catch the
close-handle-exception in start_mysql_service()? Right now I don't catch
anything in the


I do not see why the closing the handle could trigger an exception:
releasing a ressource ought to always be safe. But assuming your statement
is true and the close can't be ignored, i would augment the handle-class to
have a close method:
class raii_handle
{
public:
//constructor: obtains handle
//destructor: releases handle
~raii_handle()
{
try
{
close();
}
catch (...)
{
// either assert or log error - or just plainly ignore it.
// in places where the errorcheck is needed, you can do an
// explicit close
}
}
void close(); // could throw
};

/Peter
Jul 22 '05 #3

"Peter Koch Larsen" <pk*****@mailme.dk> wrote in message
news:F4********************@news000.worldonline.dk ...

"Eric Lilja" <er*************@yahoo.com> skrev i en meddelelse
news:cr**********@news.island.liu.se...
Hello, in my program I have a function (pseudo code):

void start_mysql_service()
{
obtain handle

start mysql service using handle

if start fails close handle and throw an exception containing error
description

else just close handle and return
}
First off, the handle should be wrapped into a class providing RAII.

Now your code is reduced into this simpler:

class raii_handle
{
public:
//constructor: obtains handle
//destructor: releases handle
};

void start_mysql_service()
{
handle_class hc(); // constructor allocates


You mean handle_class hc; I presume. The above declares a function as you
know, a silly typo I've made myself numerous times.

start mysql service using handle
}


So no matter if the service is successfully started or not, the handle
needs to be closed to prevent leakage. But the function that closes the
handle may also throw. How should I handle that? catch the
close-handle-exception in start_mysql_service()? Right now I don't catch
anything in the
I do not see why the closing the handle could trigger an exception:
releasing a ressource ought to always be safe. But assuming your statement
is true and the close can't be ignored, i would augment the handle-class
to have a close method:


Well, it can fail at least and I (the programmer) want to be notified of
such errors so I'm making close_service_handle() throw. I like the idea of
wrapping the handle in a class..it gets more robust because you cannot
forget to close the handle, the destructor will do it for you. But if the
destructor fails to close the handle I want to know somehow..an exception is
not a good idea I guess..I like the logging idea you proposed.

class raii_handle
{
public:
//constructor: obtains handle
//destructor: releases handle
~raii_handle()
{
try
{
close();
}
catch (...)
{
// either assert or log error - or just plainly ignore it.
// in places where the errorcheck is needed, you can do an //
explicit close
}
}
void close(); // could throw
};

/Peter


Thanks for your help

/ Eric
Jul 22 '05 #4

"Eric Lilja" <er*************@yahoo.com> skrev i en meddelelse
news:cr**********@news.island.liu.se...

"Peter Koch Larsen" <pk*****@mailme.dk> wrote in message
news:F4********************@news000.worldonline.dk ...

"Eric Lilja" <er*************@yahoo.com> skrev i en meddelelse
news:cr**********@news.island.liu.se...

[snip]
Now your code is reduced into this simpler:

class raii_handle
{
public:
//constructor: obtains handle
//destructor: releases handle
};

void start_mysql_service()
{
handle_class hc(); // constructor allocates
You mean handle_class hc; I presume. The above declares a function as you
know, a silly typo I've made myself numerous times.


Of course.

[snip]

I do not see why the closing the handle could trigger an exception:
releasing a ressource ought to always be safe. But assuming your
statement is true and the close can't be ignored, i would augment the
handle-class to have a close method:


Well, it can fail at least and I (the programmer) want to be notified of
such errors so I'm making close_service_handle() throw. I like the idea of
> wrapping the handle in a class..it gets more robust because you cannot

forget to close the handle, the destructor will do it for you. But if the
destructor fails to close the handle I want to know somehow..an exception
is not a good idea I guess..I like the logging idea you proposed.


Right. But do examine WHY the call might fail. I've never seen errors of a
type that couldn't either be ignored or replaced by an assertion.
[snip]
Jul 22 '05 #5

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

Similar topics

8
by: Craig Thomson | last post by:
I was wondering what people do with text provided by the user in a form. Some cleaning needs to be done at some stage if you are going to be putting it in a database or displaying it etc. But when...
7
by: Creative Acceleration | last post by:
Hi all, I need to convert the Query Strings into Clean URLs, Found some articles on PHP and Apache server.. How do we it them with ASP ?? Thanks Kart
0
by: John . | last post by:
I have a Windows XP/SP2 home system that due to some memory modules errors has partially corrupted the .NET 1.1 environment. I re-registered some modules using KB82463 and KB281679 using...
0
by: Sreejumon[MVP] | last post by:
Hi, For controls haveing text property (textbox, lablel etc) you can use ControlName.Text = String.Empty to clear the contents. For databind controls like repeater, datagrid, dalalist etc you...
0
by: Ed J | last post by:
How do I "clean" intermediate/executable files out of a VB.Net project? I added both the "Clean Solution" and "Clean Selection" commands to the Visual .NET 2003 menus, but both options stay...
2
by: roel.schreurs | last post by:
Sometimes, I want to archive or transport a solution. In such cases, I would like to remove all redundant files from the solution, i.e. all compiler output. The documention describes the command...
5
by: Rob R. Ainscough | last post by:
I have a moderately sized web application (30 pages, and 20 DLLs) that takes 10-20 minutes to "Build Solution" after I do a "Clean Solution" -- this is ONLY apparent after a "Clean Solution" I...
3
by: Nick Gilbert | last post by:
Hi, In my VS.NET 2005, if I choose Build Clean Solution, the BIN folder is not touched. Shouldn't it delete all the dll and pdb files in that folder first? Instead, I'm finding I have to do it...
0
by: Christopher | last post by:
How can I make a vc++ project erase its $(OutDir) upon using the clean command? I tryed Properties->Configuration Properties->General->Extensions to Delete on Clean Add $(ObjDir) And it...
8
by: plenty900 | last post by:
Hello, I'm trying to build a large project, and I've found that when something doesn't compile properly, and I attempt to re-build it, Visual C++ Express doesn't make a new attempt. So then I...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
1
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.