假设我在函数中有几行代码。每一行都有可能抛出Null Pointer Exception
。我想让我的函数继续执行行,即使抛出异常!但每次发生异常时,我的函数都会立即返回调用函数而不执行下面的行。我在函数中try-catch
异常。以下是我的代码:
// 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()));
即使发生异常,我想让我的函数继续执行,比如第6行。我将如何解决这个问题?我可以在每一行之前设置一个条件,检查它是否为空,但这需要花费太多时间 - 我有300行代码......
答案 0 :(得分:3)
我使用enum
来表示这样的模式。
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.
}
}
}
答案 1 :(得分:1)
这样做。一些周到的全局查找和替换以及一些键盘弯头(手指?)润滑脂将使所有300行转换为此形式的速度比您想象的要快得多。
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);
}
}