0

What is the scope of a try/catch? Essentially I am deserializing some objects and creating new references to store them in. Once they are loaded, I tried to use a method in the references but am given the below compiling error.

        try{
        ObjectInputStream is = new ObjectInputStream(new FileInputStream("saveGame.ser"));
        gameCharacter oneRestore = (gameCharacter) is.readObject();
        gameCharacter twoRestore = (gameCharacter) is.readObject();
        gameCharacter threeRestore = (gameCharacter) is.readObject();
    } catch (Exception ex) {ex.printStackTrace();}

    System.out.println("One's type is: " + oneRestore.getType());
    System.out.println("Two's type is: " + twoRestore.getType());
    System.out.println("Three's type is: " + threeRestore.getType());

The compilation error is:

Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
oneRestore cannot be resolved
twoRestore cannot be resolved
threeRestore cannot be resolved
sherrellbc
  • 4,650
  • 9
  • 48
  • 77
  • 6
    Like any other block of code, its scope starts on the `{` brace and finish in the `}` brace (similar to an `if-else` or a `for` loop block). Note that the compiler error is **very clear** about this. – Luiggi Mendoza Jun 03 '13 at 03:21
  • I realize what the error was, and how to fix it. However, I did not realize the very obvious scope = {-> <-} – sherrellbc Jun 03 '13 at 03:24

2 Answers2

8

Scope is always the enclosing {}. You need the variable declared before the try.

John3136
  • 28,809
  • 4
  • 51
  • 69
  • 5
    +1, but I'd emphasize that it needs to be declared *and initialized* before the `try`, since Java won't let you read a local variable that may not have been initialized, even if it's in scope. – ruakh Jun 03 '13 at 03:24
  • Fun fact: In Java, you can start a new block `{` `}` anywhere. It doesn't have to be an if/while/do/try or anything, you can start one anywhere writing a single statement would be ok. – Patashu Jun 03 '13 at 03:29
  • Why must references be initialized when created? In my solution I just initialized them to point to nothing(null), but what is the point of this? Wouldn't a call to a null reference just throw a NullPointerException? – sherrellbc Jun 03 '13 at 03:31
  • @sherrellbc See [my post](http://stackoverflow.com/a/16888704/1751640). – Mordechai Jun 03 '13 at 03:52
2

The scope is within the try block. In cases like this you need to declare the variable before the try block and use a flag to verify whether the variables were set before accessing them as follows:

gameCharacter oneRestore=null;
gameCharacter twoRestore=null;
gameCharacter threeRestore=null;
boolean wasRead = true;

try{
ObjectInputStream is = new ObjectInputStream(new FileInputStream("saveGame.ser"));
oneRestore = (gameCharacter) is.readObject();
twoRestore = (gameCharacter) is.readObject();
threeRestore = (gameCharacter) is.readObject();
} catch (Exception ex) {
wasRead=false;
ex.printStackTrace();
}

if (wasRead) {
System.out.println("One's type is: " + oneRestore.getType());
System.out.println("Two's type is: " + twoRestore.getType());
System.out.println("Three's type is: " + threeRestore.getType());
}

BTW, it is recommended to start a class name with a capital, hence gameCharacter -> GameCharacter looks more desirable to Java programmers.

Narbhakshi
  • 354
  • 3
  • 11
Big Ali
  • 217
  • 1
  • 8
  • This is essentially what I did, but why must the references be initialized? I used null as well, but what is the purpose? Wouldn't calling a null reference simply throw a NullPointerException? – sherrellbc Jun 03 '13 at 03:32
  • 1
    Java expects references to be initialized. When you declare a reference in between a try block, it is unknown to the rest of your code. Also, when you declare it outside the try block but initialize it within the try block Java cannot ensure it will be initialized as try block are not meant to complete; that is the execution might exit before the end of the block leaving the references non-initialized. – Big Ali Jun 03 '13 at 03:36