hy,
I've a question concerning exception-handling in c++:
is there a possibility to catch any exception (I know one can do that by
"catch(...)") and display its default-error-message (like in Java)?
or do you have to create your own exceptions and make your own
error-messages?
I would only need the error-number or something like a short description
which exception occured, and subsequently I want to write it to a logfile.
Therefore I want to log every exception, and not a specific exception I had
to define beforehand.
hope you understand my problem,
thx in advance,
ekim 7 2678
"Ekim" <th************@gmx.net> wrote in message
news:2p************@uni-berlin.de... hy,
I've a question concerning exception-handling in c++: is there a possibility to catch any exception (I know one can do that by "catch(...)") and display its default-error-message (like in Java)? or do you have to create your own exceptions and make your own error-messages?
No, there isn't. You can only catch an exception if you know its type,
or you
can use the "catch(...)" form. Exceptions are made to postpone error
handling
up to a point where there's enough information to do something
meaningful.
I would only need the error-number or something like a short description which exception occured, and subsequently I want to write it to a logfile. Therefore I want to log every exception, and not a specific exception I had to define beforehand.
Ok, I'll admit. If the exceptions have a common base class, you can
catch them
by a reference to that class (and all the standard exceptions derive
from
std::exception). However, note that if there are user-defined
exceptions that don't
have std::exceptions as a base class, your catch( std::exception& e )
won't match.
Vladimir Ciobanu
"Vladimir Ciobanu" <rh***@rdsnet.ro> wrote in message
news:cg**********@domitilla.aioe.org... "Ekim" <th************@gmx.net> wrote in message news:2p************@uni-berlin.de... hy,
I've a question concerning exception-handling in c++: is there a possibility to catch any exception (I know one can do that by "catch(...)") and display its default-error-message (like in Java)? or do you have to create your own exceptions and make your own error-messages?
No, there isn't. You can only catch an exception if you know its type, or you can use the "catch(...)" form. Exceptions are made to postpone error handling up to a point where there's enough information to do something meaningful.
I would only need the error-number or something like a short description which exception occured, and subsequently I want to write it to a logfile. Therefore I want to log every exception, and not a specific exception I had to define beforehand.
Ok, I'll admit. If the exceptions have a common base class, you can catch them by a reference to that class (and all the standard exceptions derive from std::exception). However, note that if there are user-defined exceptions that don't have std::exceptions as a base class, your catch( std::exception& e ) won't match.
Vladimir Ciobanu
hy vladimir,
thx for your immediate response. so if I got you right, I could catch
std::exception and put the error-messages into my logfile like this:
catch (std::exception& e)
{
cout << "Exception: " << e.what();
}
is this std::exception thrown for all "normal" errors like bad memory
allocation, wrong filename when opening a file and so on? or which
exceptions does this one cover?
what's more, is it enough to handle that let's say in the main-function, and
what do I have to specify in all the other methods in order to make them
throw any appearing exception (something like : throw())?
"Ekim" <th************@gmx.net> wrote in message
news:2p************@uni-berlin.de...
[snip] so if I got you right, I could catch std::exception and put the error-messages into my logfile like this:
catch (std::exception& e) { cout << "Exception: " << e.what(); }
Yes. But you will lose the additional information the type would give
you.
However, if you are just trying to log the error message and re-throw
it,
this is fine as long as you realize the requirement is that every
exception
must inherit from std::exception. See below for more details. is this std::exception thrown for all "normal" errors like bad memory allocation, wrong filename when opening a file and so on? or which exceptions does this one cover?
In the C++ standard all the exceptions listed under 19.1[3] derive
from
std::exception. These are:
logic_error, domain_error, invalid_argument; length_error,
out_of_range,
runtime_error, range_error, overflow_error, underflow_error and
bad_alloc (18.4.2.1) - of course, all in the std namespace.
To my knowledge, these is a complete list of exceptions in the C++
standard.
As I said in the previous post, this will only catch exceptions that
are derived
from std::exception (and std::exception itself, of course).
what's more, is it enough to handle that let's say in the main-function, and what do I have to specify in all the other methods in order to make them throw any appearing exception (something like : throw())?
If by that you mean "if I catch an exception at some point, but decide
not to handle
it, how do I re-throw it, you just use the throw expression without an
operand
( just: throw; ) as stated in 15.1[6].
Vladimir Ciobanu
"Vladimir Ciobanu" <rh***@rdsnet.ro> wrote in message
news:cg**********@domitilla.aioe.org... "Ekim" <th************@gmx.net> wrote in message news:2p************@uni-berlin.de... [snip] so if I got you right, I could catch std::exception and put the error-messages into my logfile like this:
catch (std::exception& e) { cout << "Exception: " << e.what(); }
Yes. But you will lose the additional information the type would give you. However, if you are just trying to log the error message and re-throw it, this is fine as long as you realize the requirement is that every exception must inherit from std::exception. See below for more details.
is this std::exception thrown for all "normal" errors like bad memory allocation, wrong filename when opening a file and so on? or which exceptions does this one cover?
In the C++ standard all the exceptions listed under 19.1[3] derive from std::exception. These are:
logic_error, domain_error, invalid_argument; length_error, out_of_range, runtime_error, range_error, overflow_error, underflow_error and bad_alloc (18.4.2.1) - of course, all in the std namespace.
To my knowledge, these is a complete list of exceptions in the C++ standard. As I said in the previous post, this will only catch exceptions that are derived from std::exception (and std::exception itself, of course).
what's more, is it enough to handle that let's say in the main-function, and what do I have to specify in all the other methods in order to make them throw any appearing exception (something like : throw())?
If by that you mean "if I catch an exception at some point, but decide not to handle it, how do I re-throw it, you just use the throw expression without an operand ( just: throw; ) as stated in 15.1[6].
Vladimir Ciobanu
thanks a lot so far - I understand quite a bit more of exceptions now. howev
er, I do not understand why I still get an error-message when executing the
following program:
extern "C" BOOL WINAPI DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID
lpReserved) // normal dll-main-function
{
try
{
FILE *stream;
stream = fopen( "ThisFileDoesNotExistnot.txt", "r" ); // try
to force an error (open file for reading that does not exist)
fprintf(stream, "%s\n", "trying to produce an error");
fclose(stream);
switch(dwReason)
// normal dll-main-function
{
case DLL_PROCESS_ATTACH:
g_hInst = hInstance;
break;
case DLL_PROCESS_DETACH:
break;
}
}
catch(...)
// catch any exception
{
FILE *stream;
stream = fopen( "MyException.txt", "a" );
fprintf(stream, "%s\n", "DllMain - general exception(...) occured");
// write exception-info to logfile
fclose(stream);
}
return TRUE;
}
As explained a little in the comments, I try to force an error at will by
opening a file for reading that does not exist. In the catch-block I catch
any exception with catch(...) and in there I just write an error-message
into a logfile. Nothingtheless, at execution time an error-window pops up
with a message like "debug assertion failed!" (the error is definitely from
the fopen-statement as I intended).
Why does this error-message still pop up although I try to catch all
possible exceptions? My topmost aim is to avoid queer errors and sudden
program terminations like this - instead, I want to have a message written
into my logfile and terminate the program in a normal way.
Did I make something wrong at catching (...) or what's wrong at all with my
code?
thx again in advance,
ekim!
On Mon, 30 Aug 2004 12:28:52 +0300
"Vladimir Ciobanu" <rh***@rdsnet.ro> wrote: I would only need the error-number or something like a short description which exception occured, and subsequently I want to write it to a logfile. Therefore I want to log every exception, and not a specific exception I had to define beforehand. Ok, I'll admit. If the exceptions have a common base class, you can catch them by a reference to that class (and all the standard exceptions derive from std::exception).
And make sure you've noticed it's a REFERENCE to std::exception.
Otherwise you'll get the object slicing problem due to the
pass-by-value-mechanism, which results in converting a derived class
type into the base one (by calling std::exception's copy constructor),
with all consequences such as losing new class members.
best regards / Gruß
Moritz Beller
--
web http://www.4momo.de
mail momo dot beller at t-online dot de
gpgkey http://gpg.notlong.com
"Ekim" <th************@gmx.net> wrote in message
news:2p************@uni-berlin.de...
[snip] stream = fopen( "ThisFileDoesNotExistnot.txt", "r" ); // try // to force an error (open file for reading that does not exist)
fopen / fclose / fprintf / etc functions are "inherited" by C++ from
the C
library. As C doesn't have exceptions, these functions will not throw
in case of error. Instead, compiled in debuging mode, they produce a
runtime assertion error.
Not even C++ IOstreams (ifstream / ofstream) do not throw exceptions.
Streams have fail bits which can be checked for errors.
catch(...) // catch any exception { FILE *stream; stream = fopen( "MyException.txt", "a" ); fprintf(stream, "%s\n", "DllMain - general exception(...) occured"); // write exception-info to logfile fclose(stream); } return TRUE; }
As I said, there's nothing to catch. There's no exception thrown -
it's just a
runtime assertion that causes the program to stop from executing.
My topmost aim is to avoid queer errors and sudden program terminations like this - instead, I want to have a message written into my logfile and terminate the program in a normal way.
One way would be checking the stream's fail bits and throw an
exception.
Consider you need to read a settings file that provides you with the
default
font size, color, etc. You would have a class that would open a file
stream,
reads the settings and saves them for later use. If any of the
previous operations
fail (file doesn't exist, file doesn't have the correct syntax,
settings missing or
have inconsistent values, not enough memory to store the settings,
etc), your
class would throw an exception.
If you do all that in the constructor, you would do what is known as
RAII
(Resource Acquisition Is Initialization). You can probably google the
term for
a better understanding.
Vladimir Ciobanu
"Ekim" <th************@gmx.net> wrote in message news:<2p************@uni-berlin.de>... hy,
I've a question concerning exception-handling in c++: is there a possibility to catch any exception (I know one can do that by "catch(...)") and display its default-error-message (like in Java)? or do you have to create your own exceptions and make your own error-messages?
No and no.
A catch-all clause doesn't let you do much of anything with what you
catch, largely because there may not be anything to do -- it may not
have a default message, or much of anything else.
The usual advice, however, is to derive exception classes from those
in the standard library, and all of these DO have at an associated
string.
As such, what you probably want to do is something like catching the
library's base class, and printing out a diagnostic if you do, and
follow that by a catch-all to catch anything else, realizing that you
won't be able to do much with it.
--
Later,
Jerry.
The universe is a figment of its own imagination. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Brian Brane |
last post by:
I have properties that wrap DataRow columns as in:
public int aNumber
{ get{ return m_DataRow; }
set{ m_DataRow = value; } }
If the column happens to contain DBNull, I get a cast
exception...
|
by: John Wood |
last post by:
Even though CSC does its best to detect use of unassigned variables, it
often misses it... for example if you just declare a double in a class
without assigning a default value, it has a default...
|
by: silesius |
last post by:
I've been using VS 2003 to develop a webapplication using C#. Today I
exported the application to a remote webserver I begun experiencing
problems.
It's a simple application that retrieves some...
|
by: Andrew J. Marshall |
last post by:
I want to create a class that must receive a parameter when instantiated.
In other words, I do not want it to have a "Public Sub New()".
1) Does VB.NET create a default public constructor if I do...
|
by: Stacey Levine |
last post by:
I have a webservice that I wanted to return an ArrayList..Well the service
compiles and runs when I have the output defined as ArrayList, but the WSDL
defines the output as an Object so I was...
|
by: Nick Vaughan |
last post by:
While running some long term tests on our Broadcast SDK one of our
thread dropped out because an exception had been thrown:
Exception: System.ArgumentOutOfRangeException
Message: Load factor...
|
by: Plop69 |
last post by:
need some help on following:
xml file 1
<TEST xmlns="http://test" >
<OK>mlkddflmkj</OK>
</TEST>
xml file 2
|
by: Plop69 |
last post by:
need some help on following:
xml file 1
<TEST xmlns="http://test" >
<OK>mlkddflmkj</OK>
</TEST>
|
by: bh |
last post by:
Is there such thing as a default exception value? I'm trying to pass an
optional parameter, containing an exception back to a calling procedure. In
the pseudocode, below, what might I put in...
|
by: hanusoft |
last post by:
This is an example of editing in DataGrid and Default Paging
http://www.hanusoftware.com
Html Design Code : -
<asp:DataGrid id="DataGrid1" DataKeyField="id" runat="server" Height="224px"...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
| |