Throw an own exception

FPawlak
2020-03-02
2020-03-13
  • FPawlak - 2020-03-02

    Hi guys.
    I know that there is mechanism

    __TRY, __CATCH, __FINALLY
    

    where can I check for example whether there is divison by 0.

    But what if I want to generate my own exception in order to stop normal execution of program - for example when I am not able to allocate new memory

    __NEW() return 0 not address of allocated memory
    

    I have seen

    __THROW
    

    but when I try to use it

    __THROW(__SYSTEM.ExceptionCode.RTSEXCPT_USER_EXCEPTION_BASE);
    

    Codesys says future not implemented.

    when I try

    __SYSTEM.throwex(__SYSTEM.ExceptionCode.RTSEXCPT_USER_EXCEPTION_BASE);
    

    Codesys says unexpected token _ _ SYSTEM found

    Yes I know, instead of _ _ throw an exception I can force codesys to divide by zero which will generate an exception.
    This is the only possible solution?

    Second question. How can I put underscore in text? One underscore change formating to italics, double underscore change formating to bold.

     
  • i-campbell

    i-campbell - 2020-03-02

    To generate an exception, you need these:
    CmpApp.GetCurrent()
    CmpApp.AppGenerateException()

    List of exceptions available at:
    SysExcept.RtsExceptions
    Usually 16#2000 is good for your own exception

    To generate a less cryptic log message associated with that exception, you need
    CmpLog.LogAdd2()

    Checkout "escapes and HTML" at this page (new forum uses Apache Allura. Sourceforge also uses that, so sometimes you can find info on sourceforge):
    https://forge-allura.apache.org/p/allura/wiki/markdown_syntax/#md_ex_escapes

    So I would type "\_\_" to get "__"

     
  • FPawlak - 2020-03-03

    Hi,
    thank you very much for your answer. In SysExcept there is also function SysExceptGenerateException so probablly CmpApp.AppGenerateException() is not needed.

    But now I have another problem - only some exceptions can be 'catched' witch __try __catch - for example for RtsExceptions.RTSEXCPT_DIVIDEBYZERO it works, but I can not catch e.g. SysExcept.RTSEXCPT_VENDOR_EXCEPTION_BASE.

    What wrong am I doing? Should I register the exception before?

     
  • FPawlak - 2020-03-13

    Hi,
    I'm still looking for solution how to use 16#2000 or SysExcept.RTSEXCPT_VENDOR_EXCEPTION_BASE for exception.
    The problem is, if I throw these exception even in __try __catch the execution of my PLC program stops and can't be continued.

    Please see an example below:

    PROGRAM PLC_PRG
    VAR
        exc : __SYSTEM.ExceptionCode;
    
        divisionByZero : BOOL;
        vendor : BOOL;
    END_VAR
    
    <----------------------------------------------------------------------------------------------------------------->
    
    //This can be catched
    __TRY
        SysExcept.SysExceptGenerateException(udiException := SysExcept.RtsExceptions.RTSEXCPT_DIVIDEBYZERO);
    __CATCH(exc)
        divisionByZero := TRUE;
    __ENDTRY
    
    
    //This can't be cached
    __TRY
        SysExcept.SysExceptGenerateException(udiException := SysExcept.RtsExceptions.RTSEXCPT_VENDOR_EXCEPTION_BASE);
    __CATCH(exc)
        vendor := TRUE;
    __ENDTRY
    

    When I throw RTSEXCPT_DIVIDEBYZERO then in __CATCH() clause divisionByZero is set to TRUE and execution goes further.

    But when I throw RTSEXCPT_VENDOR_EXCEPTION_BASE then in __CATCH() clause vendor is not set to TRUE, and code execution is stoped on line

    SysExcept.SysExceptGenerateException(udiException := SysExcept.RtsExceptions.RTSEXCPT_VENDOR_EXCEPTION_BASE);
    

    I am using Wago PLC - PFC200 750-8202.
    SysExcept library in version 3.5.7.0

     
  • i-campbell

    i-campbell - 2020-03-13

    I think we need to wait for SP16 to do such a thing. Currently SP15 only catches the non-vendor exceptions.

     

    Last edit: i-campbell 2020-03-13

Log in to post a comment.