473,765 Members | 2,021 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

What is the difference between exceptions and simple error handlingtechniq ues?

I've just finished reading the article below which goes into some
depth about exceptions. The article was rather lucid and so I
understand how to implement it all, the thing I'm having trouble with
though is why would I need to them? For example, I could suppress my
functions using the @ sign and then use an if statement to check if
they returned false or not. Similarly, I can also return various
integers, like -4 and -5 for errors, and check those.

Article: http://www.talkphp.com/showthread.php?t=1478

As you can see, the article gives some reasons why, but I don't think
it's quite convinced me that exceptions are the way to go. I thought
maybe they'd introduce a lot more into the error handling side of
things, but I think I am mistaken. Unless I am overlooking something?
Dec 3 '07 #1
5 2820
ad************* @gmail.com wrote:
I've just finished reading the article below which goes into some
depth about exceptions. The article was rather lucid and so I
understand how to implement it all, the thing I'm having trouble with
though is why would I need to them? For example, I could suppress my
functions using the @ sign and then use an if statement to check if
they returned false or not. Similarly, I can also return various
integers, like -4 and -5 for errors, and check those.

Article: http://www.talkphp.com/showthread.php?t=1478

As you can see, the article gives some reasons why, but I don't think
it's quite convinced me that exceptions are the way to go. I thought
maybe they'd introduce a lot more into the error handling side of
things, but I think I am mistaken. Unless I am overlooking something?
Well, They give you the ability to encapsulate your error messages
inside the code that generates them, and them do something about them
outside. It makes a neat script , for one thing.
Dec 3 '07 #2
ad************* @gmail.com wrote:
I've just finished reading the article below which goes into some
depth about exceptions. The article was rather lucid and so I
understand how to implement it all, the thing I'm having trouble with
though is why would I need to them? For example, I could suppress my
functions using the @ sign and then use an if statement to check if
they returned false or not. Similarly, I can also return various
integers, like -4 and -5 for errors, and check those.

Article: http://www.talkphp.com/showthread.php?t=1478

As you can see, the article gives some reasons why, but I don't think
it's quite convinced me that exceptions are the way to go. I thought
maybe they'd introduce a lot more into the error handling side of
things, but I think I am mistaken. Unless I am overlooking something?
Please don't multipost. Crosspost if you must post to multiple newsgroups.

Answered in alt.php.

--
=============== ===
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attgl obal.net
=============== ===

Dec 3 '07 #3
ad************* @gmail.com schrieb:
I've just finished reading the article below which goes into some
depth about exceptions. The article was rather lucid and so I
understand how to implement it all, the thing I'm having trouble with
though is why would I need to them?
The usefulness of exceptions is highly discussed and it's easy to start
flame wars over this topic. First of all, there are more ways to handle
errors than you stated: The "simple error handling techniques" are split
up into several more:

- return some magic error code integer or false if it didn't work
- use trigger_error() to raise an error
- return an error object (PEAR does this)
- (insert your approach here)

error codes
------------
These are widely used! It's the C language way of handling errors. The
problem with the error code approach is that you are responsible to
remember that a function might return an error EVERY TIME YOU USE IT.
Example: You use a function and forget the error handling. Some time
later an error happens and is not handled. Because you did not put in
the error handling it might yield totally unpredictable results at
random other positions in your project. It could take you days to find
the source of the problem.

trigger_error
--------------
These are real errors you cannot ignore. But the big problem is: You
can't handle these where they occur because the only place where you can
react is a centralized error handler. There is no way to return to the
previoud code position after handling the error. Therefore, this method
is unsuitable for any errors you want to react on and then continue.

error objects
--------------
PEAR uses a standard of returning a "PEAR_Error " object if something
went wrong. Actually, newer versions of PEAR allow you to change this
behaviour and do something else (e.g. throw an exception), but I want to
explain the error object case. Well, this is mostly a poor simulation of
exceptions: You have to check if the return value is a PEAR_Error, which
is mostly done with PEAR::isError($ result). If you do this, your code
will look very much like the code you would use if you were using
exceptions (at least it won't be any shorter). But the problem of error
codes remains: You might just forget to handle the error. This time it's
a bit less problematic because you might by chance still have the
PEAR_Error object at the error position and it will tell you were it
came from because it contains an error message.

exceptions
-----------
These are a standardized way of slapping you in the face if you forget
to handle a possible error. If they are not handled, they are just as
bad as raised errors in that your app aborts and you get to see what
happened and where it happened. Nothing won compared to trigger_error
because in an error handler you have access to the call stack too. What
you win is that you CAN handle the exception where it happens and then
continue with the execution.

As previously stated: Exceptions are a controversial topic, but there
are reasons to have them.

One particular application for exceptions is in object creation: You
can't return an error code from a constructor!! If you are instantiating
a database connection object and your DBMS is down then you will still
get an object that just won't work. If you want to check for this you
are forced to call a method like $db->isOK() or suffer later when you
try to use $db in vain. Using exceptions you just don't let the object
creation happen; you throw an exception in the constructor. No database
object, no checking required, no further possibility of falsely
believing everything worked as expected.

OLLi

--
"I can be naked in 20 seconds. That includes travel time."
[Susan, DH 204]
Dec 4 '07 #4
Thank you for your responses. I think I'm getting close to realising
their true purpose. Incidentally, how do I cross-post?
Dec 4 '07 #5
ad************* @gmail.com wrote:
Thank you for your responses. I think I'm getting close to realising
their true purpose. Incidentally, how do I cross-post?
Well, to start with, get yourself a real newsreader. This is usenet,
and Google Groups is just a poor interface. There are much better ways
to read newsgroups. Even Outlook Express (shudder) is better than
Google Groups.

--
=============== ===
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attgl obal.net
=============== ===

Dec 4 '07 #6

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

Similar topics

3
1301
by: codymanix | last post by:
I have a class and some of its operations are not valid under certains cercumstances. what if the user clicks on the button which lauches this operation? Should I use InvaldoperationException or should I derive my own Exception from System.ApplicationException? Iam not sure what to do. -- cody
4
1801
by: a_geek | last post by:
Hello, I'd like to catch all exeptions and be able to inspect them. The simple case: I know which exceptions I'll get: # standard textbook example: try: something() except ThisException, e:
16
2176
by: Einar Hst | last post by:
Hi, I'm getting into the Trace-functionality in .NET, using it to provide some much-needed logging across dlls in the project we're working on. However, being a newbie, I'm wondering if some more experienced loggers can provide me with some ideas as to how to log in a simple yet flexible manner. For instance, I'd like the code to be as uncluttered as possible by Trace statements. As an example of basic logging functionality, I've come...
13
5053
by: Jason Huang | last post by:
Hi, Would someone explain the following coding more detail for me? What's the ( ) for? CurrentText = (TextBox)e.Item.Cells.Controls; Thanks. Jason
3
12111
by: chris | last post by:
hello, I can't seem to make this work: VS2005 I have a simple program that uses a backgroundworker control to execute a long process (webservice call) if that webservice call fails, i want to raise an exception and display a messagebox. however no matter where i put the Try block to trap the error, it does not work.
669
26185
by: Xah Lee | last post by:
in March, i posted a essay “What is Expressiveness in a Computer Language”, archived at: http://xahlee.org/perl-python/what_is_expresiveness.html I was informed then that there is a academic paper written on this subject. On the Expressive Power of Programming Languages, by Matthias Felleisen, 1990. http://www.ccs.neu.edu/home/cobbe/pl-seminar-jr/notes/2003-sep-26/expressive-slides.pdf
53
3187
by: jaso | last post by:
Can you give any comments on this code? I used one goto, is it bad? #include <stdio.h> #include <stdlib.h> #include <ctype.h> #include <string.h> #include <assert.h> #define NOT_NULL 1
34
3629
by: emrahayanoglu | last post by:
Hello Everyone, Now, I'm working on a new web framework. I tried many test on the other programming languages. Then i decided to use python on my web framework project. Now i want to listen all of you. What do you want in that web framework(Easy use of Database, Easy use of XML, GUI Designer, etc...)? I'm wating your answers. Thank you for all answers...!
2
3476
by: fischermx | last post by:
Exception Catching difference between VC++ and C# In C# I have this: try { int x, y, z; x = 20; y = 0;
0
9568
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
10164
Oralloy
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...
0
9835
tracyyun
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...
1
7379
isladogs
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...
0
5277
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...
0
5423
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3926
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
2
3532
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2806
bsmnconsultancy
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...

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.