Try Catch expense and best practice

  1. 5 days ago

    Jim F

    May 14 Pre-Release Testers, Xojo Pro

    Hello,

    Normally I use Try/Catch for just one exception type and a narrow section of code. Looking at the future of Sockets they will use Try/Catch instead of checking True/False conditions. Is it better to have many Try/Catch loops in a method, or have most of the socket code in a larger scope with several catch types at the end? I assume one major reason for this change is code execution speed, not having to check for all the True/False conditions - assume all is well unless an exception is triggered.

    That being said, here is some example code:

    Dim mbTest As NEW MemoryBlock(10)
    Try
      mbTest.StringValue(9,5) = "Hello"
    Catch erData As NilObjectException
      MsgBox("Will not execute this, cause no Nil Object issues")
    Catch erData As RunTimeException
      MsgBox("Will execute this")
    Catch erData As OutOfBoundsException
      MsgBox("Yes, out of bounds; but won't execute this")
    End Try

    In the code above, Catch RunTimeException and Catch OutOfBoundsException need to switch places if you want to actually catch the Out of Bounds triggered by trying to write "ello" beyond the size of mbTest.

    Is RunTimeException the only catch ALL where something like this matters, or are their other catch types where the order is important?

    Thanks.

  2. Norman P

    May 14 Pre-Release Testers, Xojo Pro Answer Freeport, Bahamas
    Edited 5 days ago

    the only reason you need to swap the order is because RuntimeException is the parent class of all other exceptions so its like saying "catch everything" - so no more specific subtypes are caught after that

    as for "best practice" it really depends on the code, how it functions and what you expect to happen IF you catch an error or if you dont

    in a simple linear bit of code where you'd normally bail out an do nothing IF there was some error then just one try catch might suffice

    if its a loop where you are trying to do something repeatedly in the loop and want the loop to proceed even in the case you catch an error then you might have a try catch inside the loop

    Not sure there's a single "best" answer for all situations

  3. Tim P

    May 14 Pre-Release Testers Rochester, NY
    Edited 5 days ago

    @Jim F Is it better to have many Try/Catch loops in a method, or have most of the socket code in a larger scope with several catch types at the end?

    Whether you put the try catch inside a loop or outside a loop depends on the behavior you're looking for. If your loop can continue processing after handing an error, put the catch inside the loop.

    @Jim Frankland Is RunTimeException the only catch ALL where something like this matters, or are their other catch types where the order is important?

    RuntimeException is the superclass for all the others. As Norman mentioned, you're basically saying "Hey, I want everything here." Don't do that. If you're not catching a specific exception, you can't really handle it. Anything unhandled should bubble to UnhandledException -- where what is considered best practice seems to still be up for debate. My opinion is that terminating the app is best because it's an exception you haven't handled, you don't know what it is, or what effects it has. Safer to quit and start again.

    Catching exceptions is a different mindset than the older check for error codes. If you (or anyone!) have any other questions, I think this would be a great place to discuss best practices :)

  4. Norman P

    May 14 Pre-Release Testers, Xojo Pro Freeport, Bahamas

    as Tim said, do not catch RuntimeException
    catch the things you CAN handle and do want to handle and let others go by

    "catch everything" is generally NOT really what you actually want to do

    and especially not "catch everything and ignore it"

  5. Christian S

    May 15 Pre-Release Testers, Xojo Pro, XDC Speakers Germany

    Please don't catch RunTimeException.
    Catch the ones you explicit handle here.

  6. Tobias B

    May 15 Pre-Release Testers, Xojo Pro Bern, Switzerland
    Edited 5 days ago

    and if you happen to catch all exceptions, don't forget to re-raise the EndExceptions and ThreadEndExceptions. See: http://docs.xojo.com/Exception / http://docs.xojo.com/Try

or Sign Up to reply!