TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 02-27-2008, 10:27 PM   #1 (permalink)
The Acquainted
 
freenity's Avatar
 
Join Date: Feb 2008
Posts: 119
Thanks: 17
freenity is on a distinguished road
Default exception vs die

Hi
Well I have never used exceptions, but see some people do, so what is the main difference between those exceptions and die() or echo() and exit() ??
__________________
http://feudal-times.net - My PBB Game
http://gwphp.feudal-times.net - My Blog "Gaming With PHP"
freenity is offline  
Reply With Quote
Old 02-27-2008, 10:43 PM   #2 (permalink)
The Contributor
 
abiko's Avatar
 
Join Date: Feb 2008
Location: Croatia
Posts: 90
Thanks: 4
abiko is on a distinguished road
Wink

From my viewpoint:
exceptions are the way to go.
Why? Because it's the right way to debug your application if something goes wrong.

Let's use die() for example - it just stops your script. You have to put some code which you want to print out as your debug info.

Exceptions - insystem function that prints the error if any occured on specific part of your code.

Example:
PHP Code:
<?php
    
    
function test$t ) {
        
        if ( !
is_numeric$t) ) {
            throw new 
Exception ("You must use only numeric values");
        } else {
            echo 
$t;
        }
    }
    
    
test'1');
    
test'2');
    
test'aa' );
?>
Output:
Code:
12
Fatal error: Uncaught exception 'Exception' with message 'You must use only numeric values' in E:\xampplite\htdocs\test\ex.php:6 Stack trace: #0 E:\xampplite\htdocs\test\ex.php(14): test('aa') #1 {main} thrown in E:\xampplite\htdocs\test\ex.php on line 6

The second example:
PHP Code:
<?php
    
    
function test$t ) {
        
        if ( !
is_numeric$t) ) {
            throw new 
Exception ("You must use only numeric values");
        } else {
            echo 
$t;
        }
    }
    
    
    try {
    
test'a');
}

catch( 
Exception  $e ) {
        echo 
$e->getMessage();
}
?>
outputs:
You must use ony numerical values.

So the bottom line:
Error handling is improved.
You can assign your custom error message if there is an error, and you user try{} catch{}.

For me it's the way to go.

Can't wait what the others have to say
Please correct me if I was wrong about something.
__________________
Back from sysadmins to the programmers.
Send a message via ICQ to abiko Send a message via MSN to abiko
abiko is offline  
Reply With Quote
Old 02-27-2008, 10:53 PM   #3 (permalink)
The Acquainted
 
freenity's Avatar
 
Join Date: Feb 2008
Posts: 119
Thanks: 17
freenity is on a distinguished road
Default

So the execution will go directly to
PHP Code:
catch( Exception  $e ) {
        echo 
$e->getMessage(); 
if the exception occurs?
__________________
http://feudal-times.net - My PBB Game
http://gwphp.feudal-times.net - My Blog "Gaming With PHP"
freenity is offline  
Reply With Quote
Old 02-27-2008, 10:56 PM   #4 (permalink)
The Contributor
 
abiko's Avatar
 
Join Date: Feb 2008
Location: Croatia
Posts: 90
Thanks: 4
abiko is on a distinguished road
Default

Yes.
Let's say you have a multilingual site.
PHP Code:
<?php
    
    
function test$t ) {
        global 
$lang;
        if ( !
is_numeric$t) ) {
            throw new 
Exception ($lang->NUMERIC_ERROR);
        } else {
            echo 
$t;
        }
    }
    
    
    try {
    
test'a');
}

catch( 
Exception  $e ) {
       
// Some error managment
$tmpl->addVar'error'$e->getMessage() );
$tmpl->activate'errorLayout' );

}
?>
For example.
__________________
Back from sysadmins to the programmers.
Send a message via ICQ to abiko Send a message via MSN to abiko
abiko is offline  
Reply With Quote
Old 02-27-2008, 10:59 PM   #5 (permalink)
The Acquainted
 
freenity's Avatar
 
Join Date: Feb 2008
Posts: 119
Thanks: 17
freenity is on a distinguished road
Default

ok I see :)
Starting to understand it
__________________
http://feudal-times.net - My PBB Game
http://gwphp.feudal-times.net - My Blog "Gaming With PHP"
freenity is offline  
Reply With Quote
Old 02-27-2008, 11:14 PM   #6 (permalink)
The Frequenter
Newcomer 
 
xenon's Avatar
 
Join Date: Dec 2007
Location: Bucharest, Romania
Posts: 438
Thanks: 3
xenon is on a distinguished road
Default

The exceptions are not a way of ending your script. die and exit are made for that. Exceptions are used to treat various cases and act accordingly. For example:

Code:
function do_division($a, $b)
{
    if( $b == 0 )
   {
        throw new Exception('The divisor cannot be 0!');
   }

    return ($a / $b);
}

try
{
    echo 'Result: ' . do_division(2, 0);
}
catch(Exception $e)
{
    echo $e->getMessage();
    
   // use some default values
   $a = 4;
   $b = 2;
}

// continue execution ...
__________________
I have optimistic thoughts, even though sometimes (if not always) life's a bitch.
xenon is offline  
Reply With Quote
Old 02-27-2008, 11:16 PM   #7 (permalink)
The Acquainted
 
wGEric's Avatar
 
Join Date: Nov 2007
Posts: 166
Thanks: 0
wGEric is on a distinguished road
Default

What about makes using exceptions better than using trigger_error or overwriting that with your own error handler using set_error_handler or calling your own error function?

To me exceptions aren't worth it since you have to use try/catch blocks when I could just use a trigger_error or something similar right where I want the error to be spit out.

I guess if a function or something could return different types of errors then setting up multiple catch blocks would be good. But then again there's ways to do that without using exceptions.
__________________
Eric
wGEric is offline  
Reply With Quote
Old 02-27-2008, 11:55 PM   #8 (permalink)
Alan @ CIT
Member of the Month
The Frequenter
Member of the Month Top Contributor 
 
Alan @ CIT's Avatar
 
Join Date: Apr 2005
Location: South UK
Posts: 483
Thanks: 51
Alan @ CIT is on a distinguished road
Default

For all questions regarding the advantages and disadvantages of using Exceptions, I refer you to PHP5 Exception Use Guidelines :) Although it is targetted at PEAR developers it is well worth a read, particularly for folks who are new to Exceptions.

Alan
Send a message via MSN to Alan @ CIT
Alan @ CIT is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 06:09 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design