Java in Notes/Domino Explained: NotesException


The documentation on NotesException is a little scarse but basically it’s simply an exception like all the others and it inherits from java.lang.Exception via org.omg.CORBA.UserException. It inherits from org.omg.CORBA.UserException since it needs to be throwable when using remote sessions. NotesException has three constructors for you:

  • NotesException()
  • NotesException(int code, String message)
  • NotesException(int code, String message, Throwable t)

The second and third ones has an integer and a string argument which is the error code and error message as you know them from the LotusScript Error statement. The third argument of the third constructor is for throwing nested exceptions. Anyways – you can throw NotesException as you do with any exception using throw new:

try {
   ...do some stuff
} catch (NotesException e) {
   // throw new NotesException();
   // throw new NotesException(99999, "My own error code...");
   // throw new NotesException(99999, "My own error code...", e);
}

What is a little strange though is that the actual error codes for the NotesExceptions throws cannot be found in the NotesException class but is found in the lotus.domino.NotesError class. This means that if you like to test for “human understandable” error codes (e.g. NOTES_ERR_INVALID_DATE) instead of the actual error code (e.g. 4045) you need to use the constants in the NotesError interface since there isn’t a way to include the error codes like you do in LotusScript unless you implement the interface.

I would consider it bad form to implement the NotesError interface simply to get the error codes. Either use it as a static constant or, if using Java 5 outside of Notes/Domino, use static imports.

As a rule of thumb I would recommend that you do not use lotus.domino.NotesException for your own program code but rather use custom exception classes (by extending java.lang.Exception) or use standard exception classes from the JVM as you see fit. That’s how I do it anyways…

6 thoughts on “Java in Notes/Domino Explained: NotesException”

  1. Alright! Can I ask for a little help?

    Let’s make a custom NotesEmailException class that extends java.lang.Exception

    I want to test to see if Vector sendTo == null otherwise let’s throw a “No Recipient” Exception.

    Help!

    Like

  2. Below is an exception class and a class that uses it. The code isn’t brilliant but it shows how to do what you are asking…

    public class NotesEmailException extends Exception {
       public NotesEmailException() {
          super();
       }
       public NotesEmailException(String msg) {
          super(msg);
       }
       public NotesEmailException(String msg, Throwable t) {
          super(msg, t);
       }
    }
    
    import java.util.Vector;
    
    import lotus.domino.Document;
    import lotus.domino.NotesException;
    import lotus.domino.RichTextItem;
    import lotus.domino.Database;
    
    public class SendEmail {
       public void send(Database db, String subject, String msg, Vector recipients) throws NotesEmailException {
          try {
             // check recipients
             if (null == recipients || recipients.size() == 0) {
                throw new NotesEmailException("Unable to send e-mail due to no recipients.");
             }
    
             // create email
             Document doc = db.createDocument();
             doc.replaceItemValue("SendTo", recipients);
             doc.replaceItemValue("Subject", subject);
             RichTextItem rt = doc.createRichTextItem("Body");
             rt.appendText(msg);
             doc.send();
          } catch (NotesException e) {
             throw new NotesEmailException("Unable to send e-mail", e);
          }
       }
    }
    

    Like

  3. hi Mikkel,

    I’m new to java.. but i wanted to access lotus notes DB via a web app that I created using JSP.

    where can I get a java library/driver that can interface with the lotus notes DB?

    hope you can help me on this.

    Thanks a lot!

    Like

  4. Try searching your Notes installation for a file called notes.jar (in Notes 7 it’s under jvm/lib/ext). Put that on your build path and you’re good to go. For a web app you might want to look into using a session filter to ease the process of maintaining a session to Notes. I know you are new to Java but try looking at the following links:

    https://lekkimworld.com/2006/01/22/transparently_handle_notesthreads_for_web_applications_using_the_domino_java_api.html
    https://lekkimworld.com/2006/01/21/use_threadlocal_to_ease_notes_java_development.html
    https://lekkimworld.com/2006/02/02/clarifying_the_use_of_sessionfactory.html

    Alternatively IBM has released some JSP tags to ease the process of interacting with Notes from JSP. Try looking at this article on developerWorks: http://www-128.ibm.com/developerworks/lotus/library/dwintegration-jsp/

    Like

  5. hi,

     I am facing some problem with throwing the NotesException.I have created the commad line menu driven program for lotus notes.When i run the program i got exception(notesexception)…and i am re throwing exception properly.but i run the same option again my program is terminated.(i am expecting re throwing the exception and again it shows the menu.)…

    Please find the code snippet.

    public

     

     

    }

     

     

     

    }

     

     

     

    }

     

    }

    class NotesCustomException1 extends Exception {public NotesCustomException1() {super();public NotesCustomException1(String msg) {super(msg);public NotesCustomException1(String msg, Throwable t) {super(msg, t);

     

    import java.io.BufferedReader;

    import java.io.IOException;

    import java.io.InputStreamReader;

    import lotus.domino.AdministrationProcess;

    import lotus.domino.NotesException;

    import lotus.domino.NotesThread;

    import lotus.domino.Registration;

    import lotus.domino.Session;

    public class TestNotesException {

     

     public static void mainMenu()throws Exception{

      

      while (true) {

       BufferedReader reader = new BufferedReader(new InputStreamReader(

         System.in));

       

       System.out.println("nNotesTool Commands");

       int option = 0;

       System.out.println("t1.Driver/Notes Configuration Information "

         + "nt2.JVM Information"

         + " nt3.Domino Server Session Information"

         + "nt4.Authenticate to Domino Server"

         + " nt5.Create Lotus Notes Test User"

         + "nt6.Delete Lotus Notes User"

         + "nt7.Open Lotus Notes Database"

         + "nt8.Send Domino Console Command" + "nt99.Quit");

       System.out.println("Enter Command:");

       

      

        option = Integer.parseInt(reader.readLine());

        switch (option) {

        case 1:

         break;

        case 2:

        

         break;

        case 3:

             break;

        case 4:

             break;

        case 5:

         break;

        case 6:

         

         System.out

           .println("n*************** DELETE THE USER *********************n");

         System.out

           .println("Please enter user’s full name that you want delete:::");

         String userName = reader.readLine();

         System.out.println("User "

           + deleteUser(userName)

           + " is deleted from the domino Server "

           );

         break;

            case 99:

        

         System.out.println("");

         System.exit(0);

        default:

         System.out.println("Command not supported.Choose correct option");

        

        }

      }

     }

     /**

      * @param args

      * @throws Exception

      * @throws Exception

      * @throws IOException

      */

     public static void main(String[] args) throws Exception   {

      // TODO Auto-generated method stub

      try{

      

      mainMenu();

        

       /*} catch (NotesException e) {

        try{

         throw new NotesException(e.id, e.text, e.internal);

        }catch(NotesException e1){

         e1.printStackTrace();

         System.out.println(e.getCause());

         System.out.println(e.getClass());

         System.out.println(e.internal);

         //throw new NotesException(e.id, e.text, e.internal);

        }*/

     

      }catch (NotesException e) {

        System.out.println("Command not supported.Choose correct option");

        

        

        throw new NotesException(e.id, e.text, e.internal);

          } 

       catch (NumberFormatException e) {

        System.out.println("Command not supported.Choose correct option");

        

        //throw new NotesException(e.id, e.text, e.internal);

       }

       catch (NotesCustomException1 e) {

        //System.out.println("Command not supported.Choose correct option");

        //e.printStackTrace();

        //throw new NotesException(e.id, e.text, e.internal);

       }

       

       catch (Exception e) {

        System.out.println("Command not supported.Choose correct option");

        

        //throw new NotesException(e.id, e.text, e.internal);

       }finally{

        mainMenu();

       }

     

     

    }

     private static String deleteUser(String userName) throws Exception{

      // TODO Auto-generated method stub

      

      try{

       NotesThread.sinitThread();

       Session NotesSess = lotus.domino.NotesFactory.createSession();

       

       Registration tmpNReg = NotesSess.createRegistration();

       String userName1 = tmpNReg.switchToID("C:\Program Files\Lotus\Domino\data\admin.id", "novell");

      

      AdministrationProcess pAdminP = NotesSess

      .createAdministrationProcess("DomainA/NotesOrg");

      pAdminP.deleteUser(userName, true,

        AdministrationProcess.MAILFILE_DELETE_ALL, "");

      }catch(NotesException  e){

       throw new NotesCustomException1("sankar::::",e);

      }

      return userName;

     }

     }

     

     

     

    Like

  6. hi,

    I am working on command line menu driver program.

       I am facing the problem related to re throwing the exception.When i run the first time its working fine(throwing the exception).but i run the same option its terminating the pogram.Am i handling exception properly or not.?If anybody find wrong in my code please send me maik to  msankar@novell.com

     

     

     

    import java.io.BufferedReader;

    import java.io.IOException;

    import java.io.InputStreamReader;

    import lotus.domino.AdministrationProcess;

    import lotus.domino.NotesException;

    import lotus.domino.NotesThread;

    import lotus.domino.Registration;

    import lotus.domino.Session;

    public class TestNotesException {

     

     public static void mainMenu()throws Exception{

      

      while (true) {

       BufferedReader reader = new BufferedReader(new InputStreamReader(

         System.in));

       

       System.out.println("nNotesTool Commands");

       int option = 0;

       System.out.println("t1.Driver/Notes Configuration Information "

         + "nt2.JVM Information"

         + " nt3.Domino Server Session Information"

         + "nt4.Authenticate to Domino Server"

         + " nt5.Create Lotus Notes Test User"

         + "nt6.Delete Lotus Notes User"

         + "nt7.Open Lotus Notes Database"

         + "nt8.Send Domino Console Command" + "nt99.Quit");

       System.out.println("Enter Command:");

       

      

        option = Integer.parseInt(reader.readLine());

        switch (option) {

        case 1:

         break;

        case 2:

        

         break;

        case 3:

             break;

        case 4:

             break;

        case 5:

         break;

        case 6:

         

         System.out

           .println("n*************** DELETE THE USER *********************n");

         System.out

           .println("Please enter user’s full name that you want delete:::");

         String userName = reader.readLine();

         System.out.println("User "

           + deleteUser(userName)

           + " is deleted from the domino Server "

           );

         break;

            case 99:

        

         System.out.println("");

         System.exit(0);

        default:

         System.out.println("Command not supported.Choose correct option");

        

        }

      }

     }

     /**

      * @param args

      * @throws Exception

      * @throws Exception

      * @throws IOException

      */

     public static void main(String[] args) throws Exception   {

      // TODO Auto-generated method stub

      try{

      

      mainMenu();

        

       /*} catch (NotesException e) {

        try{

         throw new NotesException(e.id, e.text, e.internal);

        }catch(NotesException e1){

         e1.printStackTrace();

         System.out.println(e.getCause());

         System.out.println(e.getClass());

         System.out.println(e.internal);

         //throw new NotesException(e.id, e.text, e.internal);

        }*/

     

      }catch (NotesException e) {

        System.out.println("Command not supported.Choose correct option");

        

        

        throw new NotesException(e.id, e.text, e.internal);

          } 

       catch (NumberFormatException e) {

        System.out.println("Command not supported.Choose correct option");

        

        //throw new NotesException(e.id, e.text, e.internal);

       }

       catch (NotesCustomException1 e) {

        //System.out.println("Command not supported.Choose correct option");

        //e.printStackTrace();

        //throw new NotesException(e.id, e.text, e.internal);

       }

       

       catch (Exception e) {

        System.out.println("Command not supported.Choose correct option");

        

        //throw new NotesException(e.id, e.text, e.internal);

       }finally{

        mainMenu();

       }

     

     

    }

     private static String deleteUser(String userName) throws Exception{

      // TODO Auto-generated method stub

      

      try{

       NotesThread.sinitThread();

       Session NotesSess = lotus.domino.NotesFactory.createSession();

       

       Registration tmpNReg = NotesSess.createRegistration();

       String userName1 = tmpNReg.switchToID("C:\Program Files\Lotus\Domino\data\admin.id", "novell");

      

      AdministrationProcess pAdminP = NotesSess

      .createAdministrationProcess("DomainA/NotesOrg");

      pAdminP.deleteUser(userName, true,

        AdministrationProcess.MAILFILE_DELETE_ALL, "");

      }catch(NotesException  e){

       throw new NotesCustomException1("sankar::::",e);

      }

      return userName;

     }

     }

     

    public class NotesCustomException1 extends Exception {

     public NotesCustomException1() {

      super();

     }

     public NotesCustomException1(String msg) {

      super(msg);

     }

     public NotesCustomException1(String msg, Throwable t) {

      super(msg, t);

     }

    }

     

    Like

Comments are closed.