协助我的Try Catch

时间:2014-04-23 17:00:53

标签: java exception try-catch bukkit

如果在前一个问题中已经回答了这个问题,或者由于我自己的缺点是java的新内容,我道歉。

我无法捕捉到我正在造成的异常(模拟不良的最终用户知识)。 我正在做的是读取“config.yml”文件,但是在这种情况下我特意损坏了文件,因此无法正确解析文件,因此正确运行了代码注释。

正如您在控制台中看到的那样,堆栈跟踪仍在写入,而不是被捕获并写入文件 我怀疑它是因为我没有尝试捕获一个特定的异常,如果控制台日志中显示的是什么情况,我应该捕获什么。

通话方式:

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

方法:

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;
    }

}

控制台输出:

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

编辑: Java Doc on Method有自己的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;
    }

1 个答案:

答案 0 :(得分:0)

正如您从评论中看到的那样,这是不起作用的原因是因为异常永远不会到达try.catch,实际上它实际上是被另一个类中的基本方法捕获的。为了解决这个问题,我被迫创建了自己的类,复制了原始方法执行的操作并设置了自己的try.catch。