Jump to content

Catch error in Object COM


duzers
 Share

Recommended Posts

Hello,

I try to catch error in this code:

$AmiBroker = ObjCreate("Broker.Application");
$ChkTicker = "ABC"
$bb=$AmiBroker.Stocks($ChkTicker).Ticker

If in program Broker "ABC" exist than all is alright, but if "ABC" not exist I get error.

I translate code from Windows Stript Host, but this problem resolved by "TRY, CATCH"

AmiBroker = new ActiveXObject( "Broker.Application" );
ChkTicker = "ABC";
function Main()
{
    if( AmiBroker.Stocks.Count > 0 )
    {
        try
        {
            return AmiBroker.Stocks( ChkTicker ).Ticker == ChkTicker;
        }
        catch( e )
        {
            WScript.echo("error");
        }   
    }
    return false;       
}   
Main();

How I do this in Autoit?

THX

Edited by duzers
Link to comment
Share on other sites

@JohnOne, like this?

Global $oError = ObjEvent("AutoIt.Error", "_ErrFunc")
Func _ErrFunc()
;~  ConsoleWrite("! COM Error !  Number: 0x" & Hex($oError.number, 8) & "   ScriptLine: " & $oError.scriptline & " - " & $oError.windescription & @CRLF)
EndFunc   ;==>_ErrFunc

Global $AmiBroker = ObjCreate("Not.Broker.Application");
If $oError.number Then
    MsgBox(48, "Error", "Error number = " & Hex($oError.number, 8))
Else
    ; Maybe
EndIf

;...

♡♡♡

.

eMyvnE

Link to comment
Share on other sites

If...Then...ElseIf...Then...Else

maybe.

I try use ObjEvent and @error, but it's not work.

My code returns:

==> Object referenced outside a "With" statement.:

$bb=$AmiBroker.Stocks($ChkTicker).Ticker

$bb=$AmiBroker.Stocks($ChkTicker)^ ERROR

,when result "ABC" is not on database at broker application.

I only wont to chect it, and javascript allows to chect it by Try/Catch.

BTW if in javascript I use etc. If AmiBroker.Stocks(ChkTicker).Ticker=="ABC" it returns similar error.

Link to comment
Share on other sites

You don't know what javascript allows. You don't have that capability.

Learn basics (like 1+1=2) and then ask what you want.

...Only then you won't since you'll be seeing that your question is already answered.

ok :graduated: but I have it!, look:

Global $g_eventerror = 0  ; to be checked to know if com error occurs. Must be reset after handling.

$oMyError = ObjEvent("AutoIt.Error","MyErrFunc") ; Install a custom error handler

; Performing a deliberate failure here (object does not exist)
$oIE = ObjCreate("Broker.Application")
 $oStocks = $oIE.Stocks("ABC");
 $oSymbol = $oStocks.ticker;
if $g_eventerror then Msgbox(0,"","the previous line got an error.")

Exit


; This is my custom error handler
Func MyErrFunc()
   $HexNumber=hex($oMyError.number,8)
   Msgbox(0,"","We intercepted a COM Error !" & @CRLF & _
                "Number is: " & $HexNumber & @CRLF & _
                "Windescription is: " & $oMyError.windescription )

   $g_eventerror = 1 ; something to check for when this function returns
Endfunc

This code returns COM error info when "ABC" is not on "the list".

Thx to all

Edited by duzers
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...