How to trap for error on COM instantiation

This is a discussion on How to trap for error on COM instantiation within the PHP Language forums, part of the PHP Programming Forums category; The usual example shown for trapping for failure of COM instantiation (Windows systems) is something like (see for example http://...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 04-22-2005
Csaba Gabor
 
Posts: n/a
Default How to trap for error on COM instantiation

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

Reply With Quote
  #2 (permalink)  
Old 04-22-2005
NC
 
Posts: n/a
Default Re: How to trap for error on COM instantiation

Csaba Gabor wrote:
>
> 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 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

Reply With Quote
  #3 (permalink)  
Old 04-23-2005
Csaba Gabor
 
Posts: n/a
Default Re: How to trap for error on COM instantiation

NC wrote:
> 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) {

....
> }
> }


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

Reply With Quote
  #4 (permalink)  
Old 04-25-2005
NC
 
Posts: n/a
Default Re: How to trap for error on COM instantiation

Csaba Gabor wrote:
>
> 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?


Yes. You can throw your own exception and catch it later.

> 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
> }


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

Reply With Quote
  #5 (permalink)  
Old 04-28-2005
Csaba Gabor
 
Posts: n/a
Default Generic error/exception handler

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

Reply With Quote
  #6 (permalink)  
Old 04-28-2005
NC
 
Posts: n/a
Default Re: Generic error/exception handler

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


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

Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 11:11 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0