On exception, can I return to the line following the offender?

Prev      Next

Answer: Yes   Applet   Java Source

   import java.awt.*;
   import java.awt.event.*;
   import javax.swing.*;
   import java.applet.*;

   // Simple example showing how to return to a statement after one in which
   // an Exception is raised.  In this example objects are interrogated in
   // a loop in increasing order of index but those that raise exceptions,
   // namely objects 7, 13, and 18, are skipped over without affecting the
   // interrogation of the other objects.

   // This class provides a doit() method just so that an attempt at invoking 
   // the method on null raises an exception
   class ExcHandling_C { public void doit() { } }

   // The exceptions will be raised in the doit() method of this class
   class ExcHandling_B {
   ExcHandling_A a;

      public ExcHandling_B(ExcHandling_A a) { this.a = a; }

      public void doit () {
         ExcHandling_C c[] = new ExcHandling_C[100];
         for (int i=0 ; i < 20 ; i++) c[i] = new ExcHandling_C();
         c[7] = c[13] = c[18] = null;
      
         for (int i=0 ; i < 100 ; i++) {
            try {  
               c[i].doit();
               a.out.append(i+"\n");
            } catch (Exception e) { }
         }
      }
   }

   public class ExcHandling_A extends Applet implements ActionListener {
      JTextArea out;
      JButton button;
      ExcHandling_B b;

      public void init() {
         setLayout(new FlowLayout());
         add(new JScrollPane(out = new JTextArea(20,30)));
         add(button = new JButton("Press Me"));
         button.addActionListener(this);
         b = new ExcHandling_B(this);
      }

      public void actionPerformed (ActionEvent evt) { b.doit(); }
   }