Why shouldn't I catch NullPointerExceptions?

Discussion in 'Plugin Development' started by Choelian, Apr 23, 2013.

Thread Status:
Not open for further replies.
  1. Offline

    Choelian

    This is a chunk of code that I use to get the first line of Lore from an Item used in an EntityDamagedByEntityEvent.
    Code:
    @EventHandler
        public void onEntityDamage(EntityDamageByEntityEvent event) {
            if(event.getDamager() == null) return;
            if (!(event.getDamager() instanceof Player)) {
                return;
            }
            String LoreLine0;
            Player player = (Player) event.getDamager();
            if(player.getItemInHand() == null || player.getItemInHand().getType() == Material.AIR) return;
            ItemStack is = player.getItemInHand();
            if(is.getItemMeta().getLore() == null) return;
            LoreLine0 = is.getItemMeta().getLore().get(0);
            //do stuff
    As you can see, most of this is just making sure that the objects we want actually exist.

    Now, here I have code that does the same thing, but it much cleaner:
    Code:
        @EventHandler
        public void onEntityDamageCleaner(EntityDamageByEntityEvent event) {
            String LoreLine0;
            try {
                Player player = (Player) event.getDamager();
                LoreLine0 = player.getItemInHand().getItemMeta().getLore().get(0);
            } catch (Exception e) {
                return;
            }
            //and then do stuff
        
    Now, google tells me that catching a generic exception or a NullPointerException is considered bad form, but it didn't really tell me WHY I shouldn't do that. I'm willing to take the long route, I just want to know why I should.

    PS: I know the first code chunk could stand a little cleaning up and I believe there is another event I could use to cut one step out, but my question still stands. Why shouldn't I catch NPEs?
     
  2. Offline

    Acrobot

  3. Offline

    Comphenix

    You're not catching (or swallowing) NullPointerException, you're catching and ignoring every exception. This means that any unexpected exceptions, say an IllegalStateException, will be ignored and lost. Or perhaps the internal implementation experiences an NullPointerException, and not your code directly.

    It's incredibly difficult to debug a program if you're unsure what went wrong, as stack traces help you track down the cause of a problem. Catching something unexpected also means that your plugin could potentially keep on going with corrupted data and mess up the current game or even configuration file. This is especially a risk if you catch Exception or Throwable (god forbid).

    Aside from that, it's also significantly slower, as Acrobot mentioned.

    I would probably refactor some of your initial code into helper methods. The check for a NULL and then the type Material.AIR could be moved to a function called isValidItem or similar, and you could do the same with getLore(). That should clean things up a bit.
     
  4. Offline

    BlueMond416

    Although, if you wanted to keep this form of catching the exception instead I would recommend catching NullPointerException instead of just Exception.
     
Thread Status:
Not open for further replies.

Share This Page