TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   General (http://www.talkphp.com/general/)
-   -   exception vs die (http://www.talkphp.com/general/2362-exception-vs-die.html)

freenity 02-27-2008 10:27 PM

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() ??

abiko 02-27-2008 10:43 PM

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 :-D
Please correct me if I was wrong about something.

freenity 02-27-2008 10:53 PM

So the execution will go directly to
PHP Code:

catch( Exception  $e ) {
        echo 
$e->getMessage(); 

if the exception occurs?

abiko 02-27-2008 10:56 PM

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.

freenity 02-27-2008 10:59 PM

ok I see :)
Starting to understand it :-)

xenon 02-27-2008 11:14 PM

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 ...


wGEric 02-27-2008 11:16 PM

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.

Alan @ CIT 02-27-2008 11:55 PM

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


All times are GMT. The time now is 04:33 PM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0