0

I have a while loop. In the while loop there is a try catch. In the try a get every 2 seconds the still image from an IP camera.

But somethime i get an exception because the code doesn't get all the packets .. Now i want that when i get the exception , that my code retry the try in my code ..

Can you help me ?

while(true){

           Image image = null;
  try {

                String path = "http://10.13.8.14/media/cam0/still.jpg?res=800";

                URL url = new URL(path);
                image = ImageIO.read(url);
                Thread.sleep(2000);

            } catch (Exception e) {
                 JOptionPane.showMessageDialog(null, "image niet in orde");
            }
            ImageIcon lic = new ImageIcon(image);
            label.setIcon(lic);

        }
belmen
  • 35
  • 1
  • 7

1 Answers1

1

Put the code in a method and call the method for both the calls.

public void tryBlock() throws Exception{
        Image image=null;
        String path = "http://10.13.8.14/media/cam0/still.jpg?res=800";
        URL url = new URL(path);
        image = ImageIO.read(url);
        Thread.sleep(2000);
    }

keep calling this method until no Exception is thrown

Pooja Arora
  • 574
  • 7
  • 19
  • can you give an example ? – belmen Apr 19 '17 at 12:06
  • keep calling this method until no Exception is thrown – Pooja Arora Apr 19 '17 at 12:07
  • You should put your Try and catch block in a infinite while loop. And in try block give a terminating condition to break out of the loop. Hence it will keep on calling the try block until the terminating condition is met after catching the exception. Tell me if it works – Pooja Arora Apr 19 '17 at 12:23
  • Have a look at this also : http://stackoverflow.com/questions/13239972/how-do-you-implement-a-re-try-catch – Pooja Arora Apr 19 '17 at 12:38