473,385 Members | 1,409 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

What is the difference between exceptions and simple error handlingtechniques?

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 2793
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*******@attglobal.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*******@attglobal.net
==================

Dec 4 '07 #6

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

Similar topics

3
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...
4
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,...
16
by: Einar Høst | 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...
13
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
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...
669
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...
53
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
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...
2
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;
1
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...
0
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...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.