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

[3459 byte] By [zkajana] at [2007-11-26 13:42:31]
# 1

Ok first things first, the while loop will block until it exits. Nothing else is going to happen until that finishes. What I would do based on how I understand what you're after is create a controlling thread that holds the while loop and inside it two threads, one that does the processing and one that keeps track of the elapsed time. That way the controlling thread can be notified when the timeout thread hits its threshold and it can then bail out or whatever you need on the processing thread.

Others may feel differently but I don't have a problem with nesting try catch blocks (up to a point). The real question is whether you can do something intelligent and helpful to the user about either the inner exception or the outer one.

Hope this helps,

PS.

puckstopper31a at 2007-7-8 0:00:25 > top of Java-index,Java Essentials,Java Programming...
# 2

I see, thank you. Well, I'm not sure I understand the first part.

What I have is elsewhere in that class that implements the thread that that code is in is a

void StopRuning()

{

DataReading = false;

}

Clicking a button on my GUI dialogue calls StopRunning(), my intention being to stop the reading from that UDP port and stop also the processing of the data in question. (in fact later on I try to open up the UDP port elsewhere and ofcourse it's not available because it never closed because that thread never got to the end of it where I close the port after the catch statements are over.

What I get lost is where you say controlling thread and keeping track of elapsed time. The elapsed time is not so important, just need some way of stoping the blocking if that DataReading turns false and the easiest way I could think of was to put it on a timeout. Is there maybe some way to request an stopage of a read that's waiting on data directly?

Oh, and thanks for the advice on the nesting, all the books I've been reading have no mention of it so I was wondering.

Message was edited by:

zkajan

zkajana at 2007-7-8 0:00:25 > top of Java-index,Java Essentials,Java Programming...