View Single Post
Old 02-27-2008, 10:43 PM   #2 (permalink)
abiko
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