Best way to handle SQLException?

Discussion in 'Spigot Plugin Development' started by ultraMLG1108, Jul 11, 2018.

  1. Hi there. I'm not too sure how I should handle an SQLException for the following code. I'm not sure if a try/catch statement is the best way to go, but I don't know of any other ways. Any suggestions? (I've removed some unneeded code)

    Code (Java):
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if(cmd.getName().equalsIgnoreCase("teleport") || cmd.getName().equalsIgnoreCase("tp")) {

                    Player player = (Player) sender;
                    if(!player.hasPermission("vysioncore.teleport.self")) {
                        player.sendMessage(ChatColor.RED + "You don't have permission to use this command.");
                    } else {
                        if(args.length < 1) {
                            player.sendMessage(ChatColor.RED + "You must specify at least one player.");
                            return false;
                        } else if(args.length == 1) {
                            Player toPlayer = Bukkit.getPlayer(args[0]);
                            try {
                                if(playerDb.isTpToggled(toPlayer.getUniqueId())) {
                                    player.sendMessage(ChatColor.RED + "You cannot teleport to " + toPlayer.getName() + ".");
                            } catch(SQLException e) {
                                plugin.getLogger().log(Level.SEVERE, "Could not check if player has teleportation disabled due to an SQLException", e);
                            return true;
                        } else if(args.length == 2) {
                            if(!player.hasPermission("vysioncore.teleport.other")) {
                                player.sendMessage(ChatColor.RED + "You don't have permission to teleport others.");
                            } else {
                                Player first = Bukkit.getPlayer(args[0]);
                                Player second = Bukkit.getPlayer(args[1]);
                        } else if(args.length > 2) {
                            player.sendMessage(ChatColor.RED + "Too many arguments!");
                            player.sendMessage(ChatColor.RED + "/tp <player> [otherPlayer]");
                return true;
            return false;
  2. Never do MySql on the main server thread. You are blocking the thread which results in a TPS loss.

    As far as MySql errors go, there are a lot of checks you can do to make sure the query works before you actually run it. But in the end it's going to be the 'try catch' that gets all the errors that might be thrown due to unavoidable stuff, although I am not quite sure what could go wrong with a query like this one.
  3. I usually wrap the sql exception to a custom runtime exception eg. DBException extends IllegalStateException.

    Code (Text):
       catch SQLException e
           throw new DBException(e.getMessage(), e)
    This allows you to catch the exception where appropriate and attempt to go around the exception, or notify the user, without having to write try-catch blocks or throws every time you use the database method call.