Connecting Tech Pros Worldwide Forums | Help | Site Map

How to trap for error on COM instantiation

Csaba Gabor
Guest
 
Posts: n/a
#1: Jul 17 '05
The usual example shown for trapping for failure of COM instantiation
(Windows systems) is something like (see for example
http://at2.php.net/manual/en/class.com.php):

$word = new COM("word.application") or
die("Unable to instantiate Word");

This is all well and good, but if you are using a command
line interface (CLI) version of PHP 5 then you might wish to
have a little more sophisticated error handling. Specifically,
I have a function that looks like so:

function getWinDir() {
$result = "";
$oScript = new COM("MSScriptControl.ScriptControl");
...
return $result;
}

How do I trap for a COM instantion error here? In particular,
if it errors, I want to return ":Error" so the calling function
can deal with it. I do not want the program to abort. Nothing
I've tried seems to work. For example, suppose I'm having a bad
spelling day and leave off the final l to ensure an error.
If I insert an @ in the line, it will suppress the error reporting
and the program will also halt. And PHP doesn't like me trying

$oScript = new COM("MSScriptControl.ScriptContro") or
($result = ":Error");

I've also tried using set_error_handler and changing the
error level reporting with error_reporting and varying
$oldIni = ini_set("com.autoregister_verbose", 0); // $oldIni is 1
none of which has produced any joy.

Whenever the @ is there, I get a silent failure and a full stop.
If it's missing it also stops with the following message:
Fatal error: Uncaught exception 'com_exception' with message
'Failed to create COM object `MSScriptControl.ScriptContro':
Invalid syntax' in C:\Testing\DirFind.php:18


Thanks for any tips,
Csaba Gabor from Vienna


NC
Guest
 
Posts: n/a
#2: Jul 17 '05

re: How to trap for error on COM instantiation


Csaba Gabor wrote:[color=blue]
>
> I have a function that looks like so:
>
> function getWinDir() {
> $result = "";
> $oScript = new COM("MSScriptControl.ScriptControl");
> ...
> return $result;
> }
>
> How do I trap for a COM instantion error here?[/color]

In PHP 5, potentially fatal COM errors result in the COM
extension throwing instances of the class com_exception.
So your script needs to catch those exceptions:

function getWinDir() {
try {
$oScript = new COM("MSScriptControl.ScriptControl");
$oScript->call_a_method();
$oScript->call_another_method();
...
return $result;
} catch (com_exception $e) {
return array('errorCode' => $e->getCode(),
'errorMessage' => $e->getMessage(),
'errorFile' => $e->getFile(),
'errorLine' => $e->getLine());
}
}

Cheers,
NC

Csaba Gabor
Guest
 
Posts: n/a
#3: Jul 17 '05

re: How to trap for error on COM instantiation


NC wrote:[color=blue]
> In PHP 5, potentially fatal COM errors result in the COM
> extension throwing instances of the class com_exception.
> So your script needs to catch those exceptions:
>
> function getWinDir() {
> try {
> ...
> } catch (com_exception $e) {[/color]
....[color=blue]
> }
> }[/color]

NC,

thank you, Thank You, THANK YOU! This was a majorly
nice way to go into the weekend. I had seen the comment
about Exceptions in http://php.net/com but I never used this
try / catch construct in PHP before and glossed over it.
Thanks again for pointing it out.

I have a follow up question about try / catch in general.
Is there a way (this has nothing to do with COM) that I can
use this contruct to catch all errors in the same place?

Example: the following code strikes me as an inelegant
workaround. Really, I'm looking for a default catch_all.

$err = ":Error";
try {
... some code which might error in unknown ways ...
$err = ""
} catch (Exception $e) { // this line doesn't work (it's ignored)
// since we haven't defined Exception, we'll never wind up here
}
if ($err) { // hacky was of compensating
// evidently, there was an error
}

Thanks,
Csaba

NC
Guest
 
Posts: n/a
#4: Jul 17 '05

re: How to trap for error on COM instantiation


Csaba Gabor wrote:[color=blue]
>
> I have a follow up question about try / catch in general.
> Is there a way (this has nothing to do with COM) that I can
> use this contruct to catch all errors in the same place?[/color]

Yes. You can throw your own exception and catch it later.
[color=blue]
> Example: the following code strikes me as an inelegant
> workaround. Really, I'm looking for a default catch_all.
>
> $err = ":Error";
> try {
> ... some code which might error in unknown ways ...
> $err = ""
> } catch (Exception $e) { // this line doesn't work (it's ignored)
> // since we haven't defined Exception, we'll never wind up here
> }
> if ($err) { // hacky was of compensating
> // evidently, there was an error
> }[/color]

This can be replaced with something like this:

try {
// ... some code which might error in unknown ways ...
if ($something_is_wrong) {
$error_message = 'Error: Something is wrong...';
$error_code = 666;
throw new Exception($error_message, $error_code);
}
} catch (Exception $e) {
// Now you can catch your own exception just as you
// would be catching any other...
}

Cheers,
NC

Csaba Gabor
Guest
 
Posts: n/a
#5: Jul 17 '05

re: How to trap for error on COM instantiation


I seem to have messed up on what I posted to you. Here is example code
that does what I was looking for - a generic catch all (or at least
catch most) error handler for PHP5. The point is that I did not want
to test for errors, nor throw exceptions. I just want to catch all
errors/exceptions that happen so I can deal with them gracefully.

<?
// see http://php.net/error_reporting
error_reporting(E_ALL);

// see http://php.net/set_error_handler
function myErrorHandler($errno, $errstr, $errfile, $errline) {
// a fifth argument (context) is passed, whether defined or not
print "Standard error in line $errline:";
print "<br>\n$errstr\n<br>";
}
set_error_handler('myErrorHandler');

// see http://php.net/exceptions
try {
$foo = 7 / 0; // execution resumes after a warning
$bar = new COM("speling.error"); // exception is thrown
// exception thrown above => line below not executed
print "<br>\nThird line of the try";
} catch (Exception $e) { // generic exception handler
print "<br>\nThrown exeption in line " . $e->getLine();
print "<br>\n" . $e->getMessage() . "\n<br>";
}
print "<br>\nEnd of routine";
?>

Csaba Gabor from Vienna

NC
Guest
 
Posts: n/a
#6: Jul 17 '05

re: How to trap for error on COM instantiation


Csaba Gabor wrote:[color=blue]
>
> I just want to catch all errors/exceptions that happen
> so I can deal with them gracefully.[/color]

Well, at this point, an error and an exception are different
beasts and must be handled differently. You can, however, try
combining two handlers into one:

function handleItAll ($arg1, $arg2=NULL, $arg3=NULL, $arg4=NULL) {
if (is_object($arg1)) {
// exception thrown; handle it
}
if (is_numeric($arg1)) {
// error triggered; handle it
}
}

set_error_handler('handleItAll');
set_exception_handler('handleItAll');

Cheers,
NC

Closed Thread