Reputation: 475
I am a Java beginner having some trouble. I have a try-catch
block where the try
portion tries to run a section of code and the catch
portion runs different code if the try
code throws any sort of error. It looks something like:
try {
<code to try>
} catch(Exception e) {
<various variable reassignments>
<run "try" code again> (what I don't know how to do)
}
I think this involves some sort of loop, which I am fairly familiar with using, but I don't know how I would format it to work in this scenario.
Upvotes: 1
Views: 1868
Reputation: 27599
You can either place your entire try/catch
block into a loop, or make a method call from the try
and catch
. Note that if you use a loop you'll need some logic in place to prevent an infinite loop if you throw an Exception
every time, even after updating your variable values.
Make a method call
String arg = "foo";
try {
Object.method(arg); // your code, taking your variable as an argument
} catch (Exception e){
arg = "bar"; // update variable value
Object.method(arg); // call your code again
}
Use a loop
while (true){
try {
// ... your code, which might throw an Exception
break; // break if there are no exceptions
} catch (Exception e){
// assign vars
}
}
Upvotes: 2
Reputation: 4145
Refactor the <code to try>
into a separate method. Call it inside both try
and catch
. This way you will achieve something like a single retry. Another option is to put the whole try catch
into a while
loop and control how many retries you want using a variable and depending on the result you could initialize different variables.
Upvotes: 1
Reputation: 1667
You can encase the entire try/catch into a for/while loop that will exit when the try successfully runs.
while (true) {
try {
<code to try>
//exit on success
} catch(Exception e) {
<various variable reassignments>
}
}
Upvotes: 0
Reputation: 4786
You can wrap it with while
loop, And then if you didn't get to the success = true
assignment and got to the catch
section, do your coding and the while
loop will run again the try-catch section.
For example:
boolean success = false;
while (!success) {
try {
<code to try>
success = true;
} catch(Exception e) {
<various variable reassignments>
}
}
Upvotes: 0
Reputation: 48404
This underlines a small architectural problem.
There's no implicit looping in try
/catch
blocks: you try and recover if an Exception
is thrown.
What you can do is, from best to worst:
Place the try
code in a method, parametrizable as needed so its behavior can change when invoked again. Then invoke that method with different arguments in the try
and catch
blocks.
Note that in this case, the invocation in your catch
block might throw that same Exception
again, which you might assume is not recoverable at that point.
Use nested try
/ catch
blocks, where you vary the procedure from your try
block within a try
block nested in your catch
block.
This is ok but less readable.
Multiple nested try
/ catch
blocks will severely reduce your readability though.
Place the whole try
/ catch
in a loop whose break condition is defined in all terminations (otherwise you might be looping forever and logging tons of stack traces)
This would be acceptable in theory but is usually the recipy for disaster over time.
Upvotes: 2
Reputation: 4540
I think you want something like:
boolean run = true;
while(run)
{
try
{
// do action
run = false;
}
catch(Exception e)
{
// do whatever actions
// when you want to break, set run = false
}
}
Upvotes: 0
Reputation: 883
You could break the loop if successful, and stay in it in case of an exception:
while (true) {
try {
<code to try>
break; // operations were successful, so leave the loop!
} catch(Exception e) {
<various variable reassignments>
// do not break, i.e. run "try" code again
}
}
Upvotes: 4