I have a try
-catch
block that I wish to break
like a switch
block but I couldn't find a recommended way of doing it. I'm fetching a lot of data in the try
-catch
block and wish to stop the fetching in between in case a certain condition is met. Just to get it working for now, I've deliberately forced the code to go into the catch
block:
int i=0;
try {
//--do stuff----
if(//-------is condition met?--------//)
i = 1/0; // divide 1 by 0 -- a definite exception
}
catch (Exception e) {//---------do nothing---------//}
Is it safe to do this or should I go for another way?
EDIT:I'm fetching some xml data(actually, a lot). Depending on the internet connection, I need to stop the parsing after sometime(time-out) rather than go through the entire stream. I go through loops but I also make some calculations later. It doesn't make any sense to calculate with incomplete data, so I would prefer to just skip the whole thing.
Exception
-- what happens if the code in thetry
unintentionally throws an exception that you mistakenly interpret as your control flow? – Burdenreturn
statement if you don't want to do anything? – Rondareturn
in the middle of the block. – Fransisthrow new Exception("Ugly GOTO hack")
. However, using exceptions for control flow is a bad idea. – Irrabreak
statement, right? – Fransisfinally
stuff). – Irra"throw new Exception("Ugly GOTO hack")"
. Anyway, came across this link... clearly, the overhead caused by stack trace is the primary deterrent and the best way is to usebreak
– Fransis