Home > Archive > PHP Language > April 2005 > How to trap for error on COM instantiation
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
How to trap for error on COM instantiation
|
|
| Csaba Gabor 2005-04-22, 3:57 pm |
| 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
| |
|
| 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
| |
| Csaba Gabor 2005-04-23, 3:55 pm |
| 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 w end. 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
| |
|
| 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
| |
|
| 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
| |
|
| 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
|
|
|
|
|