Java problem with a function

I am very new to Java and have probably misunderstood something. Basically I am just trying to add a function that formats a RichText item ( xml in this case but could be anything ). When I try to save the agent it keeps giving me an error on the line rtXML.appendText(); command. The error is 'unreported exception … ; must be caught or declaredto be thrown. The thing that confuses me the most is why the easyDateFormat process works great but the renderXML process doesn’t seem to work no matter how I try to declare and pass/declare the variables. Some help would be fantastic.

Thx much ~R

import lotus.domino.*;

import java.text.*;

import java.util.*;

public class JavaAgent extends AgentBase {

public void NotesMain() {

try {

  Session session = getSession();

  AgentContext agentContext = session.getAgentContext();



  // (Your code goes here) 

  Document cgiDoc = agentContext.getDocumentContext();



  // Some stuff happens here

  // Call the function

  String strDate = easyDateFormat( "yyyyMMdd" );

  RichTextItem xmlBody = renderXML( "XMLBody" , cgiDoc );



} catch(Exception e) {

e.printStackTrace();

}

}

// Simple date / time formatter

public String easyDateFormat (String format) {

Date today = new Date();

SimpleDateFormat formatter = new SimpleDateFormat(format);

String datenewformat = formatter.format(today);

return  datenewformat;

}

public RichTextItem renderXML (String strName, Document procDoc) {

// Add XML

RichTextItem rtXML = procDoc.createRichTextItem( strName );

rtXML.appendText( "<root><msg>Hello World.</msg></root>" );

return rtXML;

}

}

Subject: Java problem with a function

Rick -

If you look at the documentation for the RichTextItem class, you’ll see something like this:

Syntax

public void appendText(String text)

throws NotesException

Parameters

String text

The text to append.

The text in blue indicates that this method can throw a NotesException (as most all of the Notes classes do). This is commonly referred to as a “checked” exception meaning that the compiler recognizes this as a potential problem and won’t let you compile your code until you either:

declare the Exception to be thrown, or

catch the Exception in your method.

Your easyDateFormat() method throws no such exception so it compiles fine. But to get your code to compile, you’ll have to choose one of the above approaches to handling the potential exception. You can declare it to be thrown like this:

public RichTextItem renderXML (String strName, Document procDoc) throws NotesException {

// Add XML

RichTextItem rtXML = procDoc.createRichTextItem( strName );

rtXML.appendText( “Hello World.” );

return rtXML;

}

Or you can catch the exception in the method itself like this:

public RichTextItem renderXML (String strName, Document procDoc) throws NotesException {

RichTextItem rtXML = null;

// Add XML

try {

rtXML = procDoc.createRichTextItem( strName );

rtXML.appendText( “Hello World.” );

return rtXML;

} catch (NotesException ne) {

System.out.println(new StringBuffer(64).append("NotesException “).append(ne.id).append(” - ").append(ne.text));

ne.printStackTrace();

}

}

Exception handling is sort of an art form unto itself. Here is a very good article you might find helpful.

dgg

Subject: RE: Java problem with a function

Dallas,

Thank you for the detailed help you are a scalp saver. Now to find the hair that I pulled out yesterday :slight_smile:

Thx much ~R