Question

I apologize if this has been answered in previous question or its due to my own shortcomings being new at java.

I'm having trouble catching an exception that I'm causing (simulating poor end-user knowledge). What I'm doing is reading a "config.yml" file, however in this situation I specifically damaged the file so that the file cannot properly be parsed, and thus code note properly being run.

As you can see from the Console the stacktrace is still being written to it, instead of being caught and written into a file I have my suspicion that its because I'm not attempting to catch a specific exception, if this is the case with whats shown on the console log, what should i be catching.

Calling Method:

 if(fillFromConfig()){
    getLogger().info("[ConsoleFilter] Config Reloaded");
    return true; //Close True
 }

Method:

public boolean fillFromConfig(){
    try{
        this.saveDefaultConfig();//puts config into data folder if none exsist
        this.reloadConfig(); //Reloads config file back into memory
        consoleFiltering = ConsoleFilter.this.getConfig().getBoolean("consoleFiltering");//extract from config and set to variable
        filterableTag = ConsoleFilter.this.getConfig().getBoolean("filterableTag");//extract from config and set to variable
        chatLogging = ConsoleFilter.this.getConfig().getBoolean("chatLogging");//extract from config and set to variable
        playerCommandLogging = ConsoleFilter.this.getConfig().getBoolean("playerCommandLogging");//extract from config and set to variable
        dateLogSeperator = ConsoleFilter.this.getConfig().getBoolean("dateLogSeperator");//extract from config and set to variable
        nodesHashMap = new HashMap<String, Node>();
        if(this.getConfig().isConfigurationSection("Filters.Contains")){
            tempList = ConsoleFilter.this.getConfig().getConfigurationSection("Filters.Contains").getKeys(false);//extracts nodes from Filters.Contains
            for (String str: tempList){
                Node tempNode = new Node(str,
                        this.getConfig().getString("Filters.Contains."+str+".file").toString(),
                        this.getConfig().getBoolean("Filters.Contains."+str+".delete"),
                        "Contains");
                nodesHashMap.put(tempNode.getNode(), tempNode);
            }
        }
        if(this.getConfig().isConfigurationSection("Filters.Ends_With")){
            tempList = ConsoleFilter.this.getConfig().getConfigurationSection("Filters.Ends_With").getKeys(false);
            for (String str: tempList){
                Node tempNode = new Node(str,
                        this.getConfig().getString("Filters.Ends_With."+str+".file").toString(),
                        this.getConfig().getBoolean("Filters.Ends_With."+str+".delete"),
                        "Ends_With");
                nodesHashMap.put(tempNode.getNode(), tempNode);
            }
        }
        if(this.getConfig().isConfigurationSection("Filters.Equals")){
            tempList = ConsoleFilter.this.getConfig().getConfigurationSection("Filters.Equals").getKeys(false);
            for (String str: tempList){
                Node tempNode = new Node(str,
                        this.getConfig().getString("Filters.Equals."+str+".file").toString(),
                        this.getConfig().getBoolean("Filters.Equals."+str+".delete"),
                        "Equals");
                nodesHashMap.put(tempNode.getNode(), tempNode);
            }
        }
        return true;
    }catch(Exception ex){
        File file = new File(this.getDataFolder() + "/config.yml");
        if(file.exists()){
            File file2 = new File(this.getDataFolder() + "/failed_config.yml");
            file.renameTo(file2);
        }
        Date time = Calendar.getInstance().getTime();
        getLogger().info("[ConsoleFilter] Error in reading config file. See CF_Exceptions.log and contact plugin Developer.");
        logToFile(time + "-" +ex.toString(),"CF_Exceptions","Logs");
        return false;
    }

}

Console Output:

23.04 09:43:41 [Server] Startup ... 15 more
23.04 09:43:41 [Server] Startup at org.bukkit.configuration.file.YamlConfiguration.loadFromString(YamlConfiguration.java:53)
23.04 09:43:41 [Server] Startup at org.yaml.snakeyaml.Yaml.load(Yaml.java:400)
23.04 09:43:41 [Server] Startup at org.yaml.snakeyaml.Yaml.loadFromReader(Yaml.java:481)
23.04 09:43:41 [Server] Startup at org.yaml.snakeyaml.constructor.BaseConstructor.getSingleData(BaseConstructor.java:120)
23.04 09:43:41 [Server] Startup at org.yaml.snakeyaml.composer.Composer.getSingleNode(Composer.java:105)
23.04 09:43:41 [Server] Startup at org.yaml.snakeyaml.composer.Composer.composeDocument(Composer.java:122)
23.04 09:43:41 [Server] Startup at org.yaml.snakeyaml.composer.Composer.composeNode(Composer.java:159)
23.04 09:43:41 [Server] Startup at org.yaml.snakeyaml.composer.Composer.composeMappingNode(Composer.java:230)
23.04 09:43:41 [Server] Startup at org.yaml.snakeyaml.parser.ParserImpl.checkEvent(ParserImpl.java:143)
23.04 09:43:41 [Server] Startup at org.yaml.snakeyaml.parser.ParserImpl.peekEvent(ParserImpl.java:158)
23.04 09:43:41 [Server] Startup at org.yaml.snakeyaml.parser.ParserImpl$ParseBlockMappingKey.produce(ParserImpl.java:558)
23.04 09:43:41 [Server] Startup at org.yaml.snakeyaml.scanner.ScannerImpl.checkToken(ScannerImpl.java:225)
23.04 09:43:41 [Server] Startup at org.yaml.snakeyaml.scanner.ScannerImpl.needMoreTokens(ScannerImpl.java:280)
23.04 09:43:41 [Server] Startup at org.yaml.snakeyaml.scanner.ScannerImpl.stalePossibleSimpleKeys(ScannerImpl.java:465)
23.04 09:43:41 [Server] INFO ^
23.04 09:43:41 [Server] INFO # Supported Values false, true
23.04 09:43:41 [Server] Startup in 'string', line 21, column 1:
23.04 09:43:41 [Server] INFO could not found expected ':'
23.04 09:43:41 [Server] INFO ^
23.04 09:43:41 [Server] INFO chat into its own log.
23.04 09:43:41 [Server] Startup in 'string', line 20, column 1:
23.04 09:43:41 [Server] INFO Caused by: while scanning a simple key
23.04 09:43:41 [Server] Startup at net.minecraft.server.ThreadMinecraftServer.run(ThreadMinecraftServer.java:16)
23.04 09:43:41 [Server] Startup at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:659)
23.04 09:43:41 [Server] Startup at net.minecraft.server.MinecraftServer.func_71217_p(MinecraftServer.java:777)
23.04 09:43:41 [Server] Startup at net.minecraft.server.dedicated.DedicatedServer.func_71190_q(DedicatedServer.java:331)
23.04 09:43:41 [Server] Startup at net.minecraft.server.dedicated.DedicatedServer.func_71333_ah(DedicatedServer.java:374)
23.04 09:43:41 [Server] Startup at org.bukkit.craftbukkit.v1_6_R3.CraftServer.dispatchServerCommand(CraftServer.java:514)
23.04 09:43:41 [Server] Startup at org.bukkit.craftbukkit.v1_6_R3.CraftServer.dispatchCommand(CraftServer.java:533)
23.04 09:43:41 [Server] Startup at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:192)
23.04 09:43:41 [Server] Startup at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44)
23.04 09:43:41 [Server] Startup at com.dirtyredz.ConsoleFilter.ConsoleFilter.onCommand(ConsoleFilter.java:65)
23.04 09:43:41 [Server] Startup at com.dirtyredz.ConsoleFilter.ConsoleFilter.fillFromConfig(ConsoleFilter.java:177)
23.04 09:43:41 [Server] Startup at org.bukkit.plugin.java.JavaPlugin.reloadConfig(JavaPlugin.java:117)
23.04 09:43:41 [Server] Startup at org.bukkit.configuration.file.YamlConfiguration.loadConfiguration(YamlConfiguration.java:175)
23.04 09:43:41 [Server] Startup at org.bukkit.configuration.file.FileConfiguration.load(FileConfiguration.java:105)
23.04 09:43:41 [Server] Startup at org.bukkit.configuration.file.FileConfiguration.load(FileConfiguration.java:138)
23.04 09:43:41 [Server] Startup at org.bukkit.configuration.file.YamlConfiguration.loadFromString(YamlConfiguration.java:55)
23.04 09:43:41 [Server] INFO ^
23.04 09:43:41 [Server] INFO # Supported Values false, true
23.04 09:43:41 [Server] Startup in 'string', line 21, column 1:
23.04 09:43:41 [Server] INFO could not found expected ':'
23.04 09:43:41 [Server] INFO ^
23.04 09:43:41 [Server] INFO chat into its own log.
23.04 09:43:41 [Server] Startup in 'string', line 20, column 1:
23.04 09:43:41 [Server] INFO org.bukkit.configuration.InvalidConfigurationException: while scanning a simple key
23.04 09:43:41 [Server] SEVERE Cannot load plugins/ConsoleFilter/config.yml

EDIT: Java Doc on Method with its own Try,Catch:

    public static YamlConfiguration loadConfiguration(File file) {
        Validate.notNull(file, "File cannot be null");

        YamlConfiguration config = new YamlConfiguration();

        try {
            config.load(file);
        } catch (FileNotFoundException ex) {
        } catch (IOException ex) {
            Bukkit.getLogger().log(Level.SEVERE, "Cannot load " + file, ex);
        } catch (InvalidConfigurationException ex) {
            Bukkit.getLogger().log(Level.SEVERE, "Cannot load " + file , ex);
        }

        return config;
    }
Was it helpful?

Solution

As you saw from the comments the reason this was not working is because the exceptions were never reaching the try.catch, In fact that were actually being caught by a base method in another class. To fix this problem I was forced to create my own class duplicating what the original method performed and setting my own try.catch their.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top