Try/Catch Block Explanation?

Discussion in 'Plugin Development' started by Gumybearking, Aug 21, 2012.

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


    Hey Everyone!
    Today i would just want some simple help
    I have seen code like this
                catch(Exception e){
    Could someone help me better understand this, and what the purpose of it is?
    I tried looking it up, but most of it was just confusing and I know the community here is awesome
    So i would love to see an answer to this, so i know when I should use this type of block

  2. Offline


    Use exception handlers when you want to take some special action when an exception occurs. A common place to use them is when doing file I/O. For just getting started I would only worry about exception handling for writing/reading from files and databases.

    For example:

            try {
            } catch (IOException ex) {
                plugin.getLogger().log(Level.SEVERE, "Could not save config to " + customConfigFile, ex);
  3. Offline


    In simple terms, it is:

    try { // Try the next condition
        file.createNewFile(); // create a new file
    } catch (Exception e) { // if the condition failed, do this
        e.printStackTrace(); // print out the error (the long wall of text)
  4. Offline


    A try/catch tblock is used to "catch" errors that could be thrown in some methods you're calling. Catching the exception gives you the chance to recover from the error and try correcting it, or avoiding a bad situation. It also means your code won't crash at that point. One example is if you're trying to write to a file but don't have permission, catching the exception allows you to print a message saying the file could not be written to, while not charging the exception will cause your code to crash with barely any information to help debug.
  5. Offline


  6. Offline


    Another example is when you convert strings to numbers using player input that could be invalid you would use something like:

    int iSomeNumber;
    try { 
    iSomeNumber = Integer.parseInteger("<player input>");
    catch (NumberFormatException e){
    iSomeNumber = 0;
    You'll typically know when you need to use one because your IDE will hint that you should "surround with try/catch".
  7. Offline


    One way I use this is when someone reloads a file that contains information that may need to be saved, such as classes or whatever (in a plugin I'm working on):

    Sample (don't take it verbatim):
    HashMap<String, String> classes = new HashMap<String, String>();
    //the HashMap may be changed somewhere in the plugin
    //every 30 seconds, the HashMap is saved to disk
    //if someone wants to load one from file,
        HashMap<String, String> classesTmp = classes; //preserve the old HashMap in case something happens!
    try {
        //load file
        //read from file
        //update the original HashMap to include the info from file
    } catch (Exception ex) {
        //if something fails, restore the original HashMap
    Just my way of prevent the HashMap from being wiped in case the file isn't able to be loaded from. ^_^

    Also, you can specify a finally { ... } statement for code to be ran in both situations.
    String in = "1"; int num;
    try {
        num = Integer.parseInt(in);
    } catch (Exception ex) {
        num = 0;
    } finally {
        if (num != 0) {
        } else {
            //something else
Thread Status:
Not open for further replies.

Share This Page