Sometimes, I can't think of any good reason why I should have the
program's logic thrown an exception. Except for catching the exception
and printing "Uh, oh" to the screen. I also think that in most cases
there's simply no way to handle an exception properly, because what can
one do about an integer overflow? Reassign values? Restart the program?
The problem will still be existing. I think that an integer overflow is
not an exception, but sluttish programming.
Now I think printing a message "There's a bug - I'm so sorry" is much
better than some bizarre error message provided by the operating
system. Kind of better than assert, which disappears when I compile the
release version and causes the bizarre error message if the bug's still
there.
My conclusion: throwing an exception is still better than assert, for
you can always print a user friendly message to the screen. You could
even provide a dialog or something and ask the user to write down the
steps that caused the bug in a text field.
I saw some guy named "lilburne" say "Use always assert, unless you have
no other choice, but even then consider the exception to be a design
flaw to be eliminated."
Somehow, I agree. After all, if you check for an error condition, then
in some sense you expect it to happen and it is no exception
whatsoever.
Now, if you happen to have any exceptional situations and you deside to
throw an exception, this is more an excuse for poor programming, I
think. Again, what can one do about an integer overflow? Or a wrong
static_cast?
Knowing that something went wrong seems to be the only good point about
exceptions to me anyway. But then again, I am all for assert, as they
don't throw an excuse to the end user. But this has to be done, anyhow.
What do you think? 29 2499
Correction: ... as they don't throw an excuse AT the end user.
posted:
Sometimes, I can't think of any good reason why I should have the
program's logic thrown an exception.
There's isn't really a rule of thumb -- it's intuition more than anything.
My conclusion: throwing an exception is still better than assert, for
you can always print a user friendly message to the screen. You could
even provide a dialog or something and ask the user to write down the
steps that caused the bug in a text field.
Unless, of course, you hijack "assert" and make it more user-friendly.
I saw some guy named "lilburne" say "Use always assert, unless you have
no other choice, but even then consider the exception to be a design
flaw to be eliminated."
Somehow, I agree. After all, if you check for an error condition, then
in some sense you expect it to happen and it is no exception
whatsoever.
People who make blanket rules of thumb like that tend to be improficient.
What do you think?
I use "assert" to indicate that something WRONG... W-R-O-N-G has happened,
such as a letter being '7'.
I throw an exceptio when something exceptional has happened, such as when a
random number is exactly 256.
--
Frederick Gotham
Frederick Gotham wrote:
I use "assert" to indicate that something WRONG... W-R-O-N-G has happened,
such as a letter being '7'.
I throw an exceptio when something exceptional has happened, such as when a
random number is exactly 256.
Why would that warrant an exception?
Fred, show us some real code where you would throw an exception in that
instance.
I use exceptions quite a bit (e.g., in a compiler when the source code
has a syntax error), but I don't know anyone who would use an exception
the way you suggest, except in jest.
Best regards,
Tom ma*******@googl email.com wrote:
Sometimes, I can't think of any good reason why I should have the
program's logic thrown an exception. Except for catching the exception
and printing "Uh, oh" to the screen. I also think that in most cases
there's simply no way to handle an exception properly, because what can
one do about an integer overflow? Reassign values? Restart the program?
The problem will still be existing. I think that an integer overflow is
not an exception, but sluttish programming.
Now I think printing a message "There's a bug - I'm so sorry" is much
better than some bizarre error message provided by the operating
system. Kind of better than assert, which disappears when I compile the
release version and causes the bizarre error message if the bug's still
there.
My conclusion: throwing an exception is still better than assert, for
you can always print a user friendly message to the screen. You could
even provide a dialog or something and ask the user to write down the
steps that caused the bug in a text field.
I saw some guy named "lilburne" say "Use always assert, unless you have
no other choice, but even then consider the exception to be a design
flaw to be eliminated."
Somehow, I agree. After all, if you check for an error condition, then
in some sense you expect it to happen and it is no exception
whatsoever.
Now, if you happen to have any exceptional situations and you deside to
throw an exception, this is more an excuse for poor programming, I
think. Again, what can one do about an integer overflow? Or a wrong
static_cast?
Those are not examples where exceptions should be used. You use exceptions
to indicate failures that are not bugs. E.g.:
class TCP_Connection {
TCP_Connection ( some args )
: some initializations
{
try to establish the connection.
if you fail, throw an exception indicating the failure.
}
...
};
It is pretty clear that an assert would be wrong: even if your code is 100%
correct, the computers network might just be down. Also, the program can do
something in this case: catch the exception and tell the user to bring up
the network and to try again then.
The point of throwing the exception is to separate concerns: the
TCP_Conncection class could provide handling in place. However, that is
less flexible. It can become unacceptably rigid, if TCP_Connection is part
of a library. Then you want the client to be able to decide how errors
should be handled. Thus, you just throw an exception and let the client
provide the handler. That is what exceptions are designed for: postponing
the handling of resource failures.
Knowing that something went wrong seems to be the only good point about
exceptions to me anyway. But then again, I am all for assert, as they
don't throw an excuse to the end user. But this has to be done, anyhow.
Huh?
Best
Kai-Uwe Bux ma*******@googl email.com wrote:
I saw some guy named "lilburne" say "Use always assert, unless you have
no other choice, but even then consider the exception to be a design
flaw to be eliminated."
Somehow, I agree. After all, if you check for an error condition, then
in some sense you expect it to happen and it is no exception
whatsoever.
Maybe we need a new concept, in addition to exceptions we can introduce
the "impossibilitie s". Instead of failing an assertion, we throw an
impossibility, like throwing an exception but without any way to catch it
X-)
More seriously: an exception is not (for me and meny others) something I'm
sure will never happen. It's something that, for infrequent of for the way
yo handle it, is better to keep separate from the normal flow.
For example, reaching eof when reading a text file is hardly considered
exceptional, but reaching it in the middle of a field of fixed length in a
binary file is a good candidate for exception.
Maybe this can also be considered a design flaw, we must design systems when
files never are corrupted, disks never fails, users don't make mistakes...
Maybe eliminate users can be a first step in that direction?
--
Salu2
<ma*******@goog lemail.comwrote in message
news:11******** **************@ i42g2000cwa.goo glegroups.com.. .
Sometimes, I can't think of any good reason why I should have the
program's logic thrown an exception. Except for catching the exception
and printing "Uh, oh" to the screen. I also think that in most cases
there's simply no way to handle an exception properly, because what can
one do about an integer overflow? Reassign values? Restart the program?
The problem will still be existing. I think that an integer overflow is
not an exception, but sluttish programming.
Now I think printing a message "There's a bug - I'm so sorry" is much
better than some bizarre error message provided by the operating
system. Kind of better than assert, which disappears when I compile the
release version and causes the bizarre error message if the bug's still
there.
My conclusion: throwing an exception is still better than assert, for
you can always print a user friendly message to the screen. You could
even provide a dialog or something and ask the user to write down the
steps that caused the bug in a text field.
I saw some guy named "lilburne" say "Use always assert, unless you have
no other choice, but even then consider the exception to be a design
flaw to be eliminated."
Somehow, I agree. After all, if you check for an error condition, then
in some sense you expect it to happen and it is no exception
whatsoever.
Now, if you happen to have any exceptional situations and you deside to
throw an exception, this is more an excuse for poor programming, I
think. Again, what can one do about an integer overflow? Or a wrong
static_cast?
Knowing that something went wrong seems to be the only good point about
exceptions to me anyway. But then again, I am all for assert, as they
don't throw an excuse to the end user. But this has to be done, anyhow.
What do you think?
I normally very seldom throw, but I tend to use assert a bit. But there are
good reasons for using throw and catch.
One reason I am doing it is because I have a function that returns a
referernce to a map, although the key may not exist in the map. I got into
a quandry as to what do I return, how do I tell the calling method that the
key was not found, and the best solution I came up with was to throw if it
was not found, and I found out it worked rather well.
CPlayer& FindPlayer( const SOCKET Socket )
{
// Get a reference in the map for this player
map_player::ite rator it = World.Connected Players.find( Socket );
if ( it != World.Connected Players.end() )
return (*it).second;
else
throw 0;
}
// First lets see if this player is already connected.
try
{
FindPlayer( wsp.Name );
// We didn't throw, so the player name is already logged in
World.MessageBu ffer.push_back( LogMessage( "Player " +
jml::StrmConver t( wsp.Name ) + " Tried to log in twice. IP " + IP ) );
return ACCEPT_CLIENT | ( MSG_ALREADY_LOG GED_IN << 8 );
}
catch ( int )
{
}
// later
try
{
CPlayer& ThisPlayer = FindPlayer( Socket );
PlayerLeft( ThisPlayer );
}
catch ( int )
{
World.MessageBu ffer.push_back( "Unlogged in socket " +
jml::StrmConver t( Socket ) + " disconnected." );
}
Basically, I will only throw if I can catch it and do something about it,
and there is no better way to handle handle it (return a value from the
function, etc...)
Assert I use only when I want to make sure code isn't running, or that some
value is true when I'm designing. Here's one case (that is actually not
needed)
class CMap
{
private:
// No copy or assignment yet so disable by making private.
CMap ( CMap const& /*CopyThis*/ ) { assert ( false ); }; // Copy
constructor.
CMap& operator=( CMap const& /*CopyThis*/ ) { assert ( false ); }; //
Assignment.
// ...
}
I have made the copy and assignment constructors private beause this class
can not be copied (pointers, etc..) so I just threw the assert in there to
make sure somehow it was never being called (but since it's private, it
shouldn't anyway).
exceptions and assert are tools, and as such should be used when they are
appropriate. It is said that when all you have is a hammer, then everything
starts to look like a nail, but in this case we have many more tools in our
toolboxes.
On Sat, 14 Oct 2006 19:17:44 -0400, Kai-Uwe Bux wrote:
>The point of throwing the exception is to separate concerns: the TCP_Conncectio n class could provide handling in place. However, that is less flexible. It can become unacceptably rigid, if TCP_Connection is part of a library. Then you want the client to be able to decide how errors should be handled. Thus, you just throw an exception and let the client provide the handler. That is what exceptions are designed for: postponing the handling of resource failures.
>Knowing that something went wrong seems to be the only good point about exceptions to me anyway. But then again, I am all for assert, as they don't throw an excuse to the end user. But this has to be done, anyhow.
Huh?
assert is a debug aid, ie. the only purpose of assert is to find bugs
in your program. assert is a macro that is not present in the release
version of the program.
OTOH, exceptions indicate expected runtime failures and errors (but
not bugs). They need to be handled (caught) in the program somewhere.
In sum, asserts and exceptions serve different purposes and have
nothing to do with each other.
Best wishes,
Roland Pibinger
Let's say you are writing a banking program. You are using libraries to
do certain tasks so you don't have to reinvent the wheel. And in one of
the libraries there is one very nifty function:
void perform_transac tion();
Now as a user of the library what do you expect when perform_transac tion
encounters an exceptional condition such as a runtime error?
What happens if all perform_transac tion does is to prints out "Uh, oh,
blah blah blah". Not a good idea because:
1. There is no screen
2. Even if there is a screen it doesn't know what language should be used
3. It fails to stop you, the caller, from doing the next step. If your
next step is to ask the user to pay for the transaction, you will get a
customer complain.
Hope that convinces you that perform_transac tion() really shouldn't
handle the exception on its own. What it should do, is to stop the
current transaction and notify you, the caller, what just happened, so
you can handle the problem better because you know if there is a screen,
the language of the customer and how to drop the interaction etc.
Regards,
Ben
On 14 Oct 2006 16:12:44 -0700 in comp.lang.c++, "Thomas Tutone"
<Th***********@ yahoo.comwrote,
> Frederick Gotham wrote:
>I use "assert" to indicate that something WRONG... W-R-O-N-G has happened, such as a letter being '7'.
I throw an exceptio when something exceptional has happened, such as when a random number is exactly 256.
Why would that warrant an exception?
It's supposed to be between 0 and 1. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Fábio Mendes |
last post by:
I'm sorry if it's an replicate. Either my e-mail program is messing with
things or the python-list sent my msg to /dev/null. I couldn't find
anything related in previous PEP's, so here it goes a very early draft
for a new "assert" syntax:
This was inspired in Ruby's assert syntax. I'm not familiar with Ruby at
all, so the chances are that this piece of code is broken, but I think
the idea is very obvious. In Ruby, assert is simply a...
|
by: Antti Granqvist |
last post by:
Hello!
I have following object relations:
Competition 1--* Category 1--* Course
1
|
|
*
Course
|
by: BigMan |
last post by:
When should one prefer assert-ing to throwing an exception?
|
by: linq936 |
last post by:
Hi,
I have many assert() call in my code, now I am considering to replace
them with exception. The reason I want to do this change is that with
the program going bigger and bigger, it is hard to test all the corner
cases, and thus assert() does not work as good as before. The problem
is if there are something really bad which was not captured by
assert(), then in runtime, most likely the program will crash. This is
the worst user...
|
by: Rob Thorpe |
last post by:
In general, is it considered bad practice to use asserts in production
code?
What about writing a macro that does the same as assert but continues
to work regardless of the state of NDEBUG?
I can see that it would be poor style to use it for commonly
encountered errors, but what about truly exceptional errors that would
rarely if ever be encountered?
| |
by: Anonieko |
last post by:
Understanding and Using Exceptions
(this is a really long post...only read it if you (a) don't know what
try/catch is OR (b) actually write catch(Exception ex) or catch{ })
The first thing I look for when evaluating someone's code is a
try/catch block. While it isn't a perfect indicator, exception handling
is one of the few things that quickly speak about the quality of code.
Within seconds you might discover that the code author...
|
by: Jim B. Wilson |
last post by:
Am I nuts? Or only profoundly confused? I expected the this little script
to print "0":
class foo(int):
def __init__(self, value):
self = value & 0xF
print foo(0x10)
Instead, it prints "16" (at least on python 2.4.4 (Linux) and 2.5 (Wine).
|
by: werasm |
last post by:
Hi all,
Care to share your thoughts on this, or point me to
some thoughts already shared.
My thoughts are like this (from a systems point of view):
When I have logic errors outside of my system (or software)
boundaries I throw a logic error (interface specification not
met, etc). This implies the system is logically erroneous. I
|
by: Peter Morris |
last post by:
In Delphi I used to use Assert as a development aid. During debug it would
ensure that certain conditions were met but in the Release build the Asserts
were not compiled into the application. I am assuming this is the same with
..NET too? Debug.Assert isn't compiled into a Release build?
The other thing I am curious about is this
public void DoSomething(Person p)
{
if (p == null)
|
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: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
| |
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: 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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
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: 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: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |