KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > xml > xmlc > commands > ErrorHandling


1 /*
2  * Enhydra Java Application Server Project
3  *
4  * The contents of this file are subject to the Enhydra Public License
5  * Version 1.1 (the "License"); you may not use this file except in
6  * compliance with the License. You may obtain a copy of the License on
7  * the Enhydra web site ( http://www.enhydra.org/ ).
8  *
9  * Software distributed under the License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
11  * the License for the specific terms governing rights and limitations
12  * under the License.
13  *
14  * The Initial Developer of the Enhydra Application Server is Lutris
15  * Technologies, Inc. The Enhydra Application Server and portions created
16  * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
17  * All Rights Reserved.
18  *
19  * Contributor(s):
20  *
21  * $Id: ErrorHandling.java,v 1.2 2005/01/26 08:29:24 jkjome Exp $
22  */

23
24 package org.enhydra.xml.xmlc.commands;
25
26 import java.io.FileNotFoundException JavaDoc;
27 import java.io.IOException JavaDoc;
28
29 import org.enhydra.error.ChainedThrowable;
30 import org.xml.sax.SAXException JavaDoc;
31
32 /**
33  * Error handling methods for use in commands.
34  */

35 public class ErrorHandling {
36
37     /*
38      * Disallow instantiation.
39      */

40     private ErrorHandling() {
41     }
42
43     /*
44      * Print out a stack trace with special handling of SAX exceptions.
45      */

46     private static void printStackTrace(Throwable JavaDoc except) {
47         except.printStackTrace();
48
49         // If this is a chained exception, then walk the stack
50
// looking for a non-chained exception.
51
while ((except != null) && (except instanceof ChainedThrowable)) {
52             except = ((ChainedThrowable)except).getCause();
53         }
54         if ((except != null) && (except instanceof SAXException JavaDoc)) {
55             Exception JavaDoc except2 = ((SAXException JavaDoc)except).getException();
56             if (except2 != null) {
57                 printStackTrace(except2);
58             }
59         }
60     }
61     
62     /*
63      * Handle an exception, printing the approriate error
64      * message and exit the program
65      *
66      * @param except Exception or error to print.
67      * @param verbose If true, print all details of the error.
68      */

69     public static void handleException(Throwable JavaDoc except,
70                                        boolean verbose) {
71         if (except instanceof ChainedThrowable) {
72             System.err.println("Error: " + except.getMessage());
73             if (verbose) {
74                 printStackTrace(except);
75             }
76
77             // If this is result of a java.lang exception, print stack.
78
Throwable JavaDoc cause = ((ChainedThrowable)except).getCause();
79             if ((!verbose) && (cause != null)
80                 && cause.getClass().getName().startsWith("java.lang.")) {
81                 printStackTrace(cause);
82             }
83         } else if ((except instanceof FileNotFoundException JavaDoc)
84                    || (except instanceof IOException JavaDoc)) {
85             System.err.println("Error: " + except.toString());
86             if (verbose) {
87                 printStackTrace(except);
88             }
89         } else {
90             System.err.println("Error: " + except.toString());
91             printStackTrace(except);
92         }
93         System.exit(1);
94     }
95 }
96
Popular Tags