• Dear Cerberus X User!

    As we prepare to transition the forum ownership from Mike to Phil (TripleHead GmbH), we need your explicit consent to transfer your user data in accordance with our amended Terms and Rules in order to be compliant with data protection laws.

    Important: If you accept the amended Terms and Rules, you agree to the transfer of your user data to the future forum owner!

    Please read the new Terms and Rules below, check the box to agree, and click "Accept" to continue enjoying your Cerberus X Forum experience. The deadline for consent is April 5, 2024.

    Do not accept the amended Terms and Rules if you do not wish your personal data to be transferred to the future forum owner!

    Accepting ensures:

    - Continued access to your account with a short break for the actual transfer.

    - Retention of your data under the same terms.

    Without consent:

    - You don't have further access to your forum user account.

    - Your account and personal data will be deleted after April 5, 2024.

    - Public posts remain, but usernames indicating real identity will be anonymized. If you disagree with a fictitious name you have the option to contact us so we can find a name that is acceptable to you.

    We hope to keep you in our community and see you on the forum soon!

    All the best

    Your Cerberus X Team

Please explain the code

muruba

New member
CX Code Contributor
3rd Party Module Dev
Tutorial Author
Patreon Silver
Joined
Jul 5, 2017
Messages
230
upload_2017-8-23_13-39-24.png


why not just IF -> PRINT?
 
This is from ignitionx, \playniax\ignitionx\tilesystem\worldcreators\tiled\tiled.monkey

But I am mostly wondering if I am missing something in this construct... It is probably just to unify the error message outputs by formatting the error message...
 
@muruba: I do take it that you do know what a Try/Catch block is used for? Though I have to admit that Marks implementation of it is some what vague.
 
Last edited:
I was a bit confused why you need to throw an exception to only log a message, not a very orthodox way of logging a message...
 
If loading the xml data is critical to the application. i.e. it will not work without it, then you should throw an exception with a message and terminate the application.

Edit: Or trigger a debug stop so you can do a little digging.
 
Last edited:
Ok, thanks!
 
Here's a little example.
Code:
Strict

' The classes for Exception handling
' A simple base class that we can extend.
Class ExceptionMSG Extends Throwable
 
    Method Message:Void()
        Print Self.msg
    End Method
 
    ' The Protected keyword I believe isn't in the docs, but if I remember it allows a derived class to access these fields
    ' while keeping them private to those classes.
    Protected
        Field msg:String
 
End Class

' Divid by Zero check
Class DivideException Extends ExceptionMSG
    Method New(msg:String)
        Self.msg = msg
    End Method
End Class

' Issue a warning
Class MultiplyWarning Extends ExceptionMSG
    Method New(msg:String)
        Self.msg = msg
    End Method
End Class


' Function to be called
' Function to check that a number is divisible.
Function Divide:Float(num:Float, div:Float)
    If div = 0 Or num = 0 Then Throw New DivideException("Divide by zero detected")
    Return num/div
End Function

' Function to issue a warning if we are multiplying an zero value
Function Multiply:Float(num:Float, mul:Float)
    If mul = 0 Or num = 0 Then Throw New MultiplyWarning("Multiply by zero detected")
    Return num*mul
End Function


' Main program entry
Function Main:Int()
 
    ' Change values here to zero etc to see what happens
    Local d:Int = 2
    Local m:Int = 0
    Local r:Int
 
    ' Uncomment to see what happens
    ' Print Divide(12, 0) ' This will create a uncaught exception if not in a try block, so you should think about where you put your try blocks. Meaning they should be in the function/method.
 
    Try
        r = Multiply(12,m)    ' Check and issue a warning if any value is zero. It will quit the try block if there is a warning.
  
        Print "12 * m = " + r
        Print r + " divided by " + Divide(r,d)    ' A function to test that we can divide the numbers. It will give a clear message, but add a bit of over-head in speed
  
  
        ' Case test for division
        Print "Normal division (a/b): " + r/d ' This would generate a message of "Floating point exception". Not very clear is it, unless you know that a division by zero produces it.
        'Print 12/0    ' The compiler will detect constant divisions by zero. Uncomment to see
  
  
        Print "This should not be printed. If there was an error or warning."
  
    ' The catch for division will either terminate the application or enter debug mode for C++Tool and GLFW if configuration is debug.
    Catch err:DivideException
        err.Message()
        #If CONFIG="debug"
            Print "Show the try block that needs to be examined in debug mode"
            DebugStop()
        #Else
            Error(err.Message())
        #Endif
  
    ' The catch for multiplication waring. Goes into debug mode for C++Tool and GLFW if configuration is debug.
    Catch err:MultiplyWarning
        err.Message()
        #If CONFIG="debug"
            Print "Show the try block that needs to be examined in debug mode"
            DebugStop()
        #Else
            Print "Not in debug mode so any commands after the Multiply check will be ignored and the try block exited"
        #EndIf
    End
 
    Print "If you see this and no other messages, then the try block was exited without any errors."
    Return 0
End Function
 
Last edited:
Back
Top Bottom