Java Q&A
by W. David Pitt 

Example 1:

if (operationA() == A ) { exceptionHandlingOperationA(); }
if (operationB() == B ) { exceptionHandlingOperationB(); }
if (operationC() == C ) { exceptionHandlingOperationC(); }  


Example 2: 

(a)
try{ 
  operationA();
  operationB();    
  operationC(); }
catch(Exception e) { 
    catchOperationA();  }

(b)
try{
  operationA();
  operationB();
  operationC(); }
catch(NullPointerException e) {
     catchOperationA(); }
catch (Throwable e) {
     catchOperationB(); }
         

(c)
try{ 
  operationA();
  operationB();    
  operationC(); }
catch(Exception e) { 
    catchOperationA();  }
finally { cleanupOperation(); }


Example 3:
public void myMethod(Object aValue) throws NullPointerException 
{
   if (aValue == null)
      {throw new NullPointerException();}
 return;
}



Listing One
/** Install file and Email exception handler.
 * @param args java.lang.String[]
 */
public static void main(String args[]) {
    // Install file and mail handlers
    Dispatch.install( new FileHandler("c:\\temp\\errors.txt") );
    Dispatch.install( new MailHandler(dpitt@crosslogic.com));
    String aString = new String("Hello");
    try {
        // cause an exception to be thrown...   
        aString.charAt(aString.length()+1);
            }
    catch(Throwable e) {Dispatch.handle(e); }
}

Listing Two
import java.io.*;
/** Exception Handler designed to output exception info to a flat file.  */
public class FileHandler implements Handler {
    FileWriter writer = null;
/** Initialize FileWriter instance.  */
public FileHandler(String aFileName) {
    super();
    // Create file writer
    try {
        writer = new FileWriter( new File(aFileName));
    } catch(IOException e) {Dispatch.handleUsingDefault(e); }
}
protected void finalize() //throws IOException 
{
    try {
    writer.flush();
    writer.close();
    System.out.println("Finalize: file handler closed");}
    catch(IOException e) {Dispatch.handleUsingDefault(e); }
}
/** Write exception info to a file. */
public void handle(ExceptionEvent event) {
    try {
    writer.write(event.info()); }
    catch(IOException e) {Dispatch.handleUsingDefault(e); }
    
}
}

Listing Three
/** Notify on-call develoepr of exception event using the mailto: URL */
public void handle(ExceptionEvent event) {
    // Set default mail server. This could be gotten from a properties file
    System.getProperties().put("mail.host", "smtp.crosslogic.com"); 
    // Create a MailTo URL
    String url = "mailto:" + developerOnCall;
    try {   
        URL u = new URL(url);                                       
        URLConnection c = u.openConnection();   
        c.setDoInput(false);                //no input
        c.setDoOutput(true);                //there is output
        c.connect();                        //connect to mail host
        // Create writer for output
        PrintWriter mailout = 
               new PrintWriter(new OutputStreamWriter(c.getOutputStream()));
        //Write headers.
        String headerLine = 
        "From: \""+"webmaster@crosslogic.com"+"\" <formtest@crosslogic.com>";
        mailout.println(headerLine);
        headerLine = "To:  \"" + developerOnCall+ "\"";
        mailout.println(headerLine);
        mailout.println();      //blank line to end the list of headers
        mailout.println(event.info());
        mailout.close();
    } catch (java.io.IOException e) {
        Dispatch.handleUsingDefault(e);
    }   
}






1


