Hi Friends,I am new with Java and I need an exception handing for the following code.
Basically i want to get into the while loop of notes document even if any error occurs in the code. similar to the approach of On Error Resume Next in Lotus script, please help in fixing this simple code.
==========================================>
import lotus.domino.*;
public class JavaAgent extends AgentBase {
public void NotesMain() {
String f1 = "";
String f2 = "";
String f3 = "";
int a;
int b;
View view = null;
Document doc = null;
try {
Session session = getSession();
AgentContext agentContext = session.getAgentContext();
// (Your code goes here)
Database db = agentContext.getCurrentDatabase();
view = db.getView("TestException");
doc = view.getFirstDocument();
while (doc != null) { // while
f1 = "";
f2 = "";
f3 = "";
f1 = doc.getItemValueString("Field1");
f2 = doc.getItemValueString("Field2");
a = Integer.parseInt(f1);
b = Integer.parseInt(f2);
try {
System.out.println("Division:"+a*b);
}
catch (NumberFormatException e) {
System.out.println("Number format exception");
continue;
}
doc = view.getNextDocument(doc);
} // while loop
}
catch (NotesException e) {}
catch(Exception e) { e.printStackTrace();}
Subject: RE: catch Exception, the super class of all type of exceptions
Hi Byju,Thanks for your modified code. it is working fine.
can i ask you some doubts, i heard that when we have an exception we need to use the “continue” statement to get back to the loop, in the code i have not seen any continue statement.
Subject: RE: catch Exception, the super class of all type of exceptions
“continue” statement is not needed. If the Exception is caught then the program by default continues from the line after “catch” block. And in this case, the line after “catch” block is still within the loop. So the loop resumes.
Note the structure:
try{
while(){
try{
}
catch(Exception e){
}
//control comes to this line on any exception within loop.
}
}
catch(Exception e){
}
There is an external try-catch block for the agent, which is needed to catch the exceptions thrown by Domino code.
And there is an internal try-catch block which catches all errors thrown by your code within loop.