Joel Min
Joel Min

Reputation: 3457

How to keep function running even when exception is thrown?

Say I have several lines of code in a function. Every line has the possibility of throwing a Null Pointer Exception. I would like to keep my function executing down the lines even if an exception is thrown! But everytime an exception occurs my function returns to the calling function immediately without executing the lines below. I am try-catching exception within the function. Below is my code:

        // get duration
        c.set(17, format(search(doc.select("div.course-info"), "h3", "Duration").select("p").first().ownText()));

        // get start date
        c.set(46, format(doc.select("div.course-info.l-span-7").first().select("p").first().ownText()));

        // get study mode
        c.set(18, format(combine(search(doc.select("div.course-info.l-span-4"), "h3", "Study").select("li"))));

        // get career
        c.set(52, format(doc.select("div#cs-aims-objectives").first().select("div").first().html()));

        // get professional accredition
        c.set(38, format(search(doc.select("div.section-highlight"), "h3", "Professional").select("div").first().html()));

        // get scholarships
        c.set(24, format(search(doc.select("div.media-body"), "h4", "Scholarships").select("div").html()));

I would like to keep my function executing down the lines even when an exception occurs at, say line 6. How would I go about it? I could simply set a condition before each line checking if it's null but that would take too much time - I have 300 lines of code like this...

Upvotes: 0

Views: 160

Answers (2)

Matt Campbell
Matt Campbell

Reputation: 2227

This'll do it. Some thoughtful global find-and-replace along with a bit of keyboard elbow (finger?) grease will have all 300 lines converted to this form much sooner than you might think.

  public static void main(String args[]) {
    CObject c = new CObject(); // Whatever 'c' is, obtained however it needs to be
    Document doc = new Document(); // Obtained however 'doc' needs to be
    setStuff(c, doc, 0);
  }

public void setStuff(CObject c, Document doc, int location) {

int locn = location;
try {
  switch(locn) {
    case 0:
            locn++;
            c.set(17, format(search(doc.select("div.course-info"), "h3", "Duration").select("p").first().ownText()));
    case 1:
            locn++;
            c.set(46, format(doc.select("div.course-info.l-span-7").first().select("p").first().ownText()));
    case 2:
            locn++;
            c.set(18, format(combine(search(doc.select("div.course-info.l-span-4"), "h3", "Study").select("li"))));
    case 3:
            locn++;
            c.set(52, format(doc.select("div#cs-aims-objectives").first().select("div").first().html()));
    case 4:
            locn++;
            c.set(38, format(search(doc.select("div.section-highlight"), "h3", "Professional").select("div").first().html()));
    case 5:
            locn++;
            c.set(24, format(search(doc.select("div.media-body"), "h4", "Scholarships").select("div").html()));
    default:
            return;
   }
  } catch (Exception e) {
    // do whatever with e
    setStuff(c, doc, locn);
  }
 }

Upvotes: 1

OldCurmudgeon
OldCurmudgeon

Reputation: 65821

I use an enum for patterns like this.

enum Thing {

    Duration {

                @Override
                void set(Something c, Doc doc) {
                    c.set(17, format(search(doc.select("div.course-info"), "h3", "Duration").select("p").first().ownText()));
                }
            },
    StartDate {

                @Override
                void set(Something c, Doc doc) {
                    c.set(46, format(doc.select("div.course-info.l-span-7").first().select("p").first().ownText()));
                }
            },
    StudyMode {

                @Override
                void set(Something c, Doc doc) {
                    c.set(18, format(combine(search(doc.select("div.course-info.l-span-4"), "h3", "Study").select("li"))));
                }
            },
    Career {

                @Override
                void set(Something c, Doc doc) {
                    c.set(52, format(doc.select("div#cs-aims-objectives").first().select("div").first().html()));
                }
            },
    ProfessionalAccreditation {

                @Override
                void set(Something c, Doc doc) {
                    c.set(38, format(search(doc.select("div.section-highlight"), "h3", "Professional").select("div").first().html()));
                }
            },
    Scholarships {

                @Override
                void set(Something c, Doc doc) {
                    c.set(24, format(search(doc.select("div.media-body"), "h4", "Scholarships").select("div").html()));
                }
            };

    abstract void set(Something c, Doc doc);
}

public void test() {
    for (Thing t : Thing.values()) {
        try {
            t.set(c, doc);
        } catch (Exception e) {
            // Log the failure and carry on.
        }
    }
}

Upvotes: 3

Related Questions