16
picoder
8y

The legit mod of try-catch! :P

Comments
  • 1
    I relate the 'finally' with a bed or sex scene ...
  • 1
    On another note, I have found the grenade analogy to be great for explaining try-catch statements to people who don't get them.
  • 0
    @Mindstormer619 the grenade anology?
  • 2
    @TheFlx Basically an Exception is a live grenade. You may have code which can "defuse" or handle that grenade. That's the code that goes into your "catch" statements, with the "try" statements marking out the points where your code has to watch out for incoming grenades.

    Meanwhile, sometimes you may have a grenade that your code does not know how to defuse, or it may not be its job to do, or it may need additional information only available at a higher level to defuse it. So it simply "throws" the grenade at someone else who's looking for it (using "try") and catches it to defuse. Similarly you could catch a grenade and then throw it again because you look at it and realize it may not be appropriate to defuse at your level.

    The grenade times out when it reaches the outermost point and no one catches it. At that point, it becomes an Unhandled Exception, or basically "explodes".

    Hope this helps :)
  • 0
    @Mindstormer619 thanks! I knew how exceptions work but that's a great way to explain it đź‘ŚCould've used it when I was trying to understand how these things worked haha
Add Comment