1.

Solve : PHP - Exception Handling won't work!?

Answer»

I'm trying to catch a general exception in PHP using try and catch blocks.

This is my code:


$a = 5;
$b = "word";

try
{
$c = $a+$b;
}

catch (Exception $e)
{
echo "Message: addition failed."
}

echo $c;


?>

I placed a string in $b variable on purpose to test the exception handling but when I run the application I get this error:

Parse error: parse error, unexpected '{' in w:\home\localhost\www\...\phpdesigner_tmp6.php on line 7

Line 7 being the try { code.

Any idea why this happens? Is there any adjustments I have to make in php.ini for this to work? Any help is greatly appreciated.

Thanks.Looks like a very easy error to me.
You can't calculate Strings with numbers.Quote from: Treval on January 21, 2010, 11:53:43 AM

Looks like a very easy error to me.
You can't calculate Strings with numbers.

That would have been a run-time error, and in fact, I don't think it would cause a problem.

The issue is actually caused by a missing ; on the "addition failed" echo line.

Code: [Select]<?php

$a=5;
$b="word";

try
{
$c=$a+$b;
}
catch(Exception$e)
{
echo"Message:additionfailed.";
}

echo$c;


?>

it doesn't trigger the exception, but it only shows 5; since + is only defined for numbers. You'd expect that to raise an exception, but it doesn't. In fact, it doesn't even cause an error.

Even as it is now, the try...catch won't catch runtime exceptions, like division by zero; because these are (for some reason) handled by the interpreter itself; in fact, the only exceptions you can Catch with a Try BLOCK are the ones you Throw yourself.

You can cause errors to be handled as exceptions if you define your own error handler routine:

Code: [Select]function errorHandler($number, $string, $file = 'Unknown', $line = 0, $context = array())
{
if (($number == E_NOTICE) || ($number == E_STRICT))
return false;

if (!error_reporting())
return false;

throw new Exception($string, $number);

return true;
}

set_error_handler('errorHandler');

It still won't catch the issue with + and strings; although I suppose that is more the domain of a warning. It will catch things like division by zero and file not found errors, though.

EDIT: is it just me, or is my first code tag syntax highlighted?Quote
EDIT: is it just me, or is my first code tag syntax highlighted?
Yep, you forgot your PHP opening and closing tags in the second.Quote from: kpac on January 21, 2010, 12:26:38 PM
Yep, you forgot your PHP opening and closing tags in the second.

naw, I didn't forget them, since it is supposed to be inserted WITHIN existing PHP Thank you for your replies. I ended up using error suppression in my php code. I've done some research and I noticed not many people use exception handling, for the most part it is just avoided using if and else statements.

Do any of you use exception handling?I used it in my java exam. lol.
Custom error throwing and catching elsewhere.

But that's java.
I use it in Java as well.C#, VB.NET, and I used Win32 SEH (Structured Exception Handling) for a while in C++, but I haven't used C++ in quite some time. I sort of use Exceptions in VB6; one of my projects has a INFRASTRUCTURE that sort of emulates Exception handling. It obviously doesn't have it's own syntax; but it emulates it pretty well.

I haven't really used Java for a long time (back when AWT was the WAY to create User Interfaces) it had exception handling then, but I don't think I actually used it (most of my projects were pretty trivial). My favourite feature in the newer languages that have developed has got to be generics. They save a ton of typing.

With regards to PHP, I think that Exceptions are more useful when your using Classes and have an object heirarchy; most PHP pages don't really need their own set of objects, and usually errors can be handled in-line, so it's really not something you see a whole lot of.Now there's Swing in java.


Discussion

No Comment Found