Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

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


Report this thread to moderator Post Follow-up to this message
Old Post
Csaba Gabor
04-22-05 08:57 PM


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


Report this thread to moderator Post Follow-up to this message
Old Post
NC
04-22-05 08:57 PM


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 wend.  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


Report this thread to moderator Post Follow-up to this message
Old Post
Csaba Gabor
04-23-05 08:55 PM


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


Report this thread to moderator Post Follow-up to this message
Old Post
NC
04-25-05 01:55 AM


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


Report this thread to moderator Post Follow-up to this message
Old Post
NC
04-25-05 08:56 PM


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


Report this thread to moderator Post Follow-up to this message
Old Post
NC
04-28-05 01:56 PM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

PHP Language archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 07:22 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.