The problem stems from try... catch(). I'm trying to handle any exceptions thrown from my class constructor. However, I cannot catch exceptions unless the test is performed in a try block. Of course, you already know this. First, my simple code:
In the above code, I declared a Node instance. Since NewNode resides within a separate scope, it no longer exists once the try scope ends. So, how am I supposed to access NewNode after the test is performed? Any solutions to this? The exception handling within the class doesn't fix it.
Edit:
If an exception is thrown an caught within the class, is it safe to access the members?
I think maybe your example isn't fully showing what kind of issue you're having. Why do you need to access an object you created inside the try block inside the catch block? The way I see exception handling here is that you are placing some code that could fail into the try block, and handling "exceptional" circumstances inside the catch. In other words, none of what is in the try block could be presumed to have worked, as it is all meant to be code that could fail "exceptionally".
So if NewNode needs to be accessed inside of the catch block, you need to sort of "guarantee" that it won't be the part of that code that will throw an exception, in which case you may as well move it outside of the try block. Do I make sense?