Jump to content

try catch


sneskid

Recommended Posts

in Javascript one can easily do the following:

<script type="text/javascript">
try {
eval('will cause error');
}
catch(e) {
alert(e);
}
</script>

in PHP the try catch system is only for errors thrown by your code, seems like I can't catch errors thrown by PHP.
<?php
try {
eval('will cause error');
}
catch (Exception $e) {
  echo 'Caught exception: ',  $e->getMessage(), "\n";
}
?>

Sadly that wont work, really limits the whole try/catch concept.

Any other ways I can achieve a Javascript like functionality?
Link to comment
Share on other sites

please note that the [url=http://us3.php.net/manual/en/language.exceptions.php]try..catch[/url] is for php5. It won't work in php4.

You might want to get creative with the [url=http://www.php.net/die]die[/url] statement, like

[code]
$sql = "select something from table";
$blah = mysql_query($sql) or die(mysql_error());
// will run the query. if it returns a fail (false, the error that mysql encountered will be displayed
[/code]
Link to comment
Share on other sites

[quote author=konnwat link=topic=124065.msg513580#msg513580 date=1169764776]
[code]<?php
try {
eval('will cause error');
}
catch (Exception $e) {
  echo 'Caught exception: '.$e->getMessage()."\n";
}
?>[/code]

try that ^^
[/quote]

yea, that doesn't work. it won't execute anything inside the catch block, but still raises the parse error, i want to catch that error.

im using php 5.1.4
Link to comment
Share on other sites

You cant catch parse errors. In fact you can't catch php generated errors.

The best you could do would be.....

[code]
<?php
try {
  if (!@eval('will cause error')) {
    throw new exception('unknown error');
  }
} catch (Exception $e) {
  echo 'Caught exception: '.$e->getMessage()."\n";
}
?>
[/code]

Which I'm sure is not really what you want to hear. Ive always thought the same myself, not being able to catch php's exceptions.... then whats the point?
Link to comment
Share on other sites

[quote author=thorpe link=topic=124065.msg513664#msg513664 date=1169773177]
You cant catch parse errors. In fact you can't catch php generated errors.

The best you could do would be.....

[code]
<?php
try {
  if (!@eval('will cause error')) {
    throw new exception('unknown error');
  }
} catch (Exception $e) {
  echo 'Caught exception: '.$e->getMessage()."\n";
}
?>
[/code]

Which I'm sure is not really what you want to hear. Ive always thought the same myself, not being able to catch php's exceptions.... then whats the point?
[/quote]

Thanks Thorpe
Yea the PHP try/catch system is weak
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.