|
-
January 9th, 2004, 05:58 PM
#1
Exceptions
If the Registry class shown below throws an exception, I get Windows error "Unhandled Exception.". I though my catch block would get executed.
Code:
class CRegistry
{
public:
CRegistry()
{
if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, base, 0, KEY_ALL_ACCESS, &hkey_) != ERROR_SUCCESS)
{
// Can't open the registry key, so throw an exception.
throw;
// Lines beyone here are never executed.
}
}
// remainder of class not shown
};
int main()
{
try
{
Registry reg("Software\\COE\\Software\\GTSAIT");
}
catch(...)
{
AfxMessageBox("Exception caught");
}
}
-
January 9th, 2004, 07:06 PM
#2
Re: Exceptions
Originally posted by stober
If the Registry class shown below throws an exception, I get Windows error "Unhandled Exception.". I though my catch block would get executed.
from the c++ standard, 15.1 (Throwing an exception):
- 6. A throw-expression with no operand rethrows the exception being handled. The exception is reactivated with the existing temporary; no new temporary exception object is created. The exception is no longer considered to be caught; therefore, the value of uncaught_exception() will again be true.
8. If no exception is presently being handled, executing a throw-expression with no operand calls terminate()
so apparently you were wrong.
-
January 9th, 2004, 08:34 PM
#3
Yes, I know I'm wrong -- but how can I catch that exception ?
-
January 9th, 2004, 08:43 PM
#4
you don't, re read 8. the best you can do is installing an SetUnhandledExceptionFilter and continuing the execution
or throw this and catch a CRegistry
-
January 9th, 2004, 09:07 PM
#5
Originally posted by Mick
you don't, re read 8. the best you can do is installing an SetUnhandledExceptionFilter and continuing the execution
Do you know how to install A SetUnhandledExceptionFilter ?
May I ask ?
Thanks,
Regards,
homestead
-
January 9th, 2004, 09:14 PM
#6
Originally posted by Homestead
Do you know how to install A SetUnhandledExceptionFilter ?
May I ask ?
Thanks,
Regards,
homestead
why yes...do you know how to use MSDN may I ask?
-
January 9th, 2004, 09:21 PM
#7
Originally posted by Mick may I ask?
Yes, of course.
do you know how to use MSDN
I searched again and again but found nothing, I thought you knew how to install it though.
Could you tell me now ?
Regards,
homestead
-
January 9th, 2004, 09:26 PM
#8
long __stdcall bar(EXCEPTION_POINTERS* t)
{
}
SetUnhandledExceptionFilter(bar);
-
January 9th, 2004, 09:34 PM
#9
Originally posted by Mick
long __stdcall bar(EXCEPTION_POINTERS* t)
{
}
SetUnhandledExceptionFilter(bar);
Thanks a lot,
Regards,
homestead
-
January 9th, 2004, 10:11 PM
#10
Originally posted by Mick
or throw this and catch a CRegistry
Thanks -- that's exactly what I wanted to know.
-
January 9th, 2004, 10:45 PM
#11
Originally posted by Mick
you don't, re read 8. the best you can do is installing an SetUnhandledExceptionFilter and continuing the execution
or throw this and catch a CRegistry
or install a terminate() handler with set_terminate()
otherwise, just throw an exception object for catch to receive. however, throwing *this would be a serious mistake, as it's not completely constructed yet - there's no guarantee that you'll have it in any usable state to manipulate in the catch block. rather, use some class derived from a standard exception class, depending on your need. ex (on msvc might look different):
Code:
class MyExc : public std::exception
{
const char* s;
public:
MyExc(const char* s_) : s(s_){}
virtual const char* what() const throw()
{
return s;
}
};
...
try {
...
throw MyExc("some error message");
...
}
catch(std::exception& e)
{
fprintf(stderr, "%s\n", e.what() );
}
-
January 9th, 2004, 10:54 PM
#12
Originally posted by abc_coder
or install a terminate() handler with set_terminate()
which terminates...different handler...
otherwise, just throw an exception object for catch to receive. however, throwing *this would be a serious mistake, as it's not completely constructed yet -
only in the ctor as indicated.
-
January 9th, 2004, 11:02 PM
#13
let me add it's perfectly legal to throw a exception in a ctor, it's just that you have to make sure you clean up any thing you have allocated.
/that is all, move along now..
-
January 10th, 2004, 01:39 AM
#14
Originally posted by Mick
let me add it's perfectly legal to throw a exception in a ctor, it's just that you have to make sure you clean up any thing you have allocated.
I just wanted to know why you said it is perfectly legal to throw AN exception in a ctor ? Where did you learn it ?
May I ask ?
Thanks a lot
Regards,
homestead
-
January 10th, 2004, 05:46 PM
#15
Originally posted by Homestead
I just wanted to know why you said it is perfectly legal to throw AN exception in a ctor ? Where did you learn it ?
May I ask ?
Thanks a lot
Regards,
homestead
it's the intended behavior. Constructors don't return error codes so if you do a one-step (i.e. in the constructor) initialization of some resource and that fails, the most dirrect way of announcing the failure is by throwing an exception. (there are other less direct ways too, but you have to revert to C-style error checking)
also, to handle the cleanup (if you're throwing from a constructor, *this is not completely constructed and thus the destructor won't be called), constructor initializers are handy: any subobjects will be in a definite state and will be handled nicely by the exception cleanup procedure.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|