returning to code from an exception, nesting exceptions
Hello
I have a thread reading incoming UDP data and then doing something with it. The program should stop this reading either when it's closed or when a button is clicked. One problem I found is that unless I put in a timeout, the DatagramSocket read will block indefinitely until the program exists. So I put in a timeout to recheck if I've since hit the button, however how do I go back to reading if I have not. Here is the code
...
DatagramSocket client;
int someport = 1234;
bool DataReading =false;
udpPacket =new DatagramPacket(somebuffer, somelength);
try
{
client =new DatagramSocket(someport);
client.setBroadcast(true);//want to listen to anything on that port
client.setSoTimeout(2000);//after 2 seconds time out if not recieved anything
DataReading =true;//start reading. this can be set false by another thread meaning I'd want to stop reading
while(DataReading)
{
client.recieve(udpPacket);
....do stuff with the packet ...
}
}
catch (SocketTimeoutException ste)
{
//want to go back to the while(DataReading) statement... but how without GOTO?
}
catch//several other exceptions
So how to go about doing that?
What happens now (meaning without the timeout) is that the other thread will set DataReading to false, but since I'm already blocking on the read I never recheck and exit the loop. So I added the timeout but now don't know how to go back gracefuly to the start of the while.
Also, somewhat related (though still doesnt' get me there) is it possible (and if so is it considred "not a good thing to do") to nest try/exception blocks. meaning something like
try
{
//something that throws an exception type a
try
{
//something that throws an exception type b
}
catch (exception b)
{
//handle exception b
}
//more code...
}
catch (exception a)
{
//handle exception a
}
I know you can just have all catching at the outside but what if you want to get more done if it can be handled internaly? possible?
Thanks for any and all help.
Message was edited by:
zkajan

