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?
 

muruba

New member
CX Code Contributor
3rd Party Module Dev
Tutorial Author
Patreon Silver
Joined
Jul 5, 2017
Messages
230
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...
 

dawlane

Well-known member
CX Code Contributor
Joined
Jun 21, 2017
Messages
852
@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:

muruba

New member
CX Code Contributor
3rd Party Module Dev
Tutorial Author
Patreon Silver
Joined
Jul 5, 2017
Messages
230
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...
 

dawlane

Well-known member
CX Code Contributor
Joined
Jun 21, 2017
Messages
852
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:

muruba

New member
CX Code Contributor
3rd Party Module Dev
Tutorial Author
Patreon Silver
Joined
Jul 5, 2017
Messages
230
Ok, thanks!
 

dawlane

Well-known member
CX Code Contributor
Joined
Jun 21, 2017
Messages
852
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:
Top Bottom