Pls help me in java exception handling

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();}

}

}

<========================================

Thanks in Advance.

Subject: catch Exception, the super class of all type of exceptions

Try the code below.Inside the ‘while’ loop there should be try catch block. You already have this.

But catch Exception, instead of catching NumberFormatException, so that the loop will continue even if any other exceptions occur.

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

	try {

            f1 = "";

            f2 = "";

            f3 = "";

            

            f1 = doc.getItemValueString("Field1");

            f2 = doc.getItemValueString("Field2");

            a = Integer.parseInt(f1);

            b = Integer.parseInt(f2);

            

            System.out.println("Division:"+a*b);

            

	}

            catch (Exception e) {

		e.printStackTrace();		

                System.out.println("Number format exception");

                

            }

            

            doc = view.getNextDocument(doc);

        } // while loop

    }

    catch (NotesException e) {}

    catch(Exception e) { e.printStackTrace();}

}

}

Regards

Byju Joy

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.

can you please tell me why it is not necessary.

Kind Regards.

Jeena Jacob

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.

Regards

Byju Joy

Subject: RE: catch Exception, the super class of all type of exceptions

Hi Byju,Thanks for your prompt response.

I got clarified with my doubts.

Happy X’mas & New Year to you.

Thanks and Regards.

Subject: RE: catch Exception, the super class of all type of exceptions

Welcome!Merry X’mas & wonderful new year for you…