0

Please find below an example of my need. Indeed, I don't want to close the entire process because even if it throw an Exception, it can continue.

Loop

// set a lot of variables and execute some methods
for (int i = 0, i < items.length; i++) {
     // blablabla
     myMethod()
     // blablabla2
}
// some code here also

MyMethod()

// blabla
if (!found)
     throw new EndCurrentProcessException()
// blabla

EndCurrentProcessException

public void EndCurrentProcessException() {
     ??? What I'm supposed to put here to stop the loop iteration ???
}

Maybe that used throw new is not the good way to do.

I hope it is clear, if it is not, do not hesitate to ask me more informations.

Royce
  • 1,557
  • 5
  • 19
  • 44
  • 2
    `Indeed, I don't want to close the entire process` - `What I'm supposed to put here to stop the CURRENT process` So do you want to stop it or not? Its unclear what you're asking – tkausl Jan 27 '20 at 15:13
  • Do you mean stop executing `MyMethod()`? You could always just `return;`, after of course logging any errors or informing the user of the failure, etc. – sleepToken Jan 27 '20 at 15:13
  • 4
    Your exception should describe what the faulty behaviour is, not handle the problem itself. And by process, do you mean iteration in the loop? – Jack Flamp Jan 27 '20 at 15:14
  • why do you want to throw an Exception in the first place? Exceptions are used for things you don't have influence on (File IO, Network, Hardware etc.). – Raildex Jan 27 '20 at 15:15
  • You need to clarify what you mean by process, because I don't think you're using that terminology correctly. The Java program _is_ a process, so maybe you mean method? Either way, you can write custom exception handlers. How you go about it depends on the application and/or frameworks you're using. See https://stackoverflow.com/questions/1548487/java-global-exception-handler – Christopher Schneider Jan 27 '20 at 15:18
  • 1
  • Thank you all for your comments: Yes by process I mean loop iteration. I cannot used return because here it is a simple example. In the real progam I have 5 o 6 methods which are called and the process could be stopped due to a problem in the last one. – Royce Jan 27 '20 at 15:39

2 Answers2

0

Don't throw an exception. Handle the method failure whichever way you see fit.

In your example, modify the myMethod() method to return boolean true if it was successful and false if it was not:

Loop:

// set a lot of variables and execute some methods
for (int i = 0, i < items.length; i++) {
   // blablabla
   if (!myMethod()) {
       // Skip this particular ITEM...
       continue;
       // Or whatever you want.
   }
   // blablabla2
}

MyMethod():

public boolean myMethod() {
    // ... Method code ...
    if (!found) {
        return false;
    }
    // ... Possibly more Method code ...
    return true;
}
DevilsHnd - 退職した
  • 8,739
  • 2
  • 19
  • 22
0

Try to use try-catch statement.

for (int i = 0, i < items.length; i++) {
    // blablabla
    try {
        myMethod();
    } catch(EndCurrentProcessException e){
        // do something or continue;
        continue;
    }
    // blablabla2
 }
 // some code here also