How to go to next iteration

Use the continue keyword:

continue;

It'll break the current iteration and continue from the top of the loop.

Here's some further reading:

continue Keyword in Java


If you want to only print out a message (or execute some code) if an exception isn't thrown at a particular point, then put that code after the line that might throw the exception:

try {
    Socket s = new Socket(IPaddress,px);
    System.out.print("Service discovered at port: " + px + "\n");
} catch(Exception e) {
    System.out.print("Nothing\n");
}

This causes the print not to execute if an exception is thrown, since the try statement will be aborted.

Alternatively, you can have a continue statement from inside the catch:

try {
    Socket s = new Socket(IPaddress,px);
} catch(Exception e) {
    System.out.print("Nothing\n");
    continue;
}
System.out.print("Service discovered at port: " + px + "\n");

This causes all of the code after the try/catch not to execute if an exception is thrown, since the loop is explicitly told to go to the next iteration.


The keyword you're looking for is continue. By putting continue after your print statement in the catch block, the remaining lines after the end of the catch block will be skipped the next iteration will begin.