Jump to content

Catch warnings


Chris92

Recommended Posts

Is there any way to catch warnings without defining a warning handler?

Example:

try
{
$settings = parse_ini_file( 'main.ini' );
}
catch( ErrorException $e )
{
throw new ErrorException('No main configuration file found for webiste', '0003');
}

 

Only ends up throwing a warning if the file doesn't exist. I want to catch it and throw an error.

Link to comment
Share on other sites

<?php

try {
// Suppress usual errors the function would echo using @
if( ($settings = @parse_ini_file( 'main.ini' )) === FALSE )
	throw new ErrorException('No main configuration file found for webiste', '0003');
else {
	// everything's okay
}
}
catch( ErrorException $e )
{
echo 'Error found: '.$e->getMessage().' ('.$e->getCode().')';
}

?>

 

Hope that helps.

Link to comment
Share on other sites

A lot of languages have try..catch blocks, but the implementation can vary. With JavaScript for example, think of them more like PHP errors. You can't "throw" an exception in JS, but you can catch errors JS throws itself -- excluding syntax type errors of course. With PL/SQL (Oracle) there are set exceptions that you can catch within routines that are handed back to the application, for example "NO_DATA_FOUND" - ORA-01403, or you can define your own, but it's a very different definition/handling process than PHP.

 

In your case there is no point in wrapping it within a try..catch block, unless you want to escalate the exception up to a parent catch block. If parse_ini_file threw exceptions on error, then it would also make sense to use one.

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.