Ok so here is the scenario from an interview I had recently. It took me a long time, but I still couldn't figure it out. Suppose I have a standard try, catch finally block.
public int portNumber = 80 ; // not a local variable.
try {
portNumber = 80 ;
functionX() ; //call function x
.... // some other code might cause an exception
}
catch (Exception e)
{
functionY(portNumber) ; //call function y
}
Finally
{
}
public void functionX()
{
portNumber = 90 ;
}
//There is a Thread always call the code above. (the try, catch, finally part)
I have a thread which always keep calling this block of Exception handling, and if the exception happened, I want to retrieve the portNumber when the Exception happened which is 80, not the portNumber currently have, which is 90. How can I do that?
I understand in catch block is something similar to a callback, so when the program execute inside the catch block when port is 80, the thread already called this exception handling block many times, so the port is already 90 or even other numbers. But I still want functionY uses parameter as 80. I tried to use Exception class, which means e.getMessage() etc, I can't find a way to attach port 80 there. How can I make sure functionY(portNumber), the portNumber here is the portNumber when exception happens, in this case 80.