Solved

best practices for custom Exceptions

Posted on 2013-02-05
3
295 Views
Last Modified: 2013-02-11
Is it good/normal practice to call another class to do something from inside a customException?

I want to clearly identify to an executive thread when something goes wrong in a weblogic producer class.  I was thinking about uniquely catching all the various JMS exceptions, but want to throw one unique exception identifying that the weblogic interface has encountered a problem.  I was thinking about defining and throwing a custom exception, and inside that class notifying the executive/main class to do some things?  Is this the correct methodology?
0
Comment
Question by:dpalyca755
3 Comments
 
LVL 21

Expert Comment

by:Amitkumar Panchal
ID: 38858283
0
 

Author Comment

by:dpalyca755
ID: 38862631
For better context of my question:
I want to create a JMS Producer class which will continually attempt to connect if it can't connect on instantiation of the object itself or if onException is called denoting a connection failure.

Is the generic example I sketched out below the correct paradigm to do so?
---------------------------------------------------------------------
public class MyProducer implements ExceptionListener {
        public MyProducer() {
                super();
                createConnection();
        }
 
        private void createConnection() {
            try {
                try {
                     // set up connection
                     // status as successful.
                } catch (NamingException ne) {
                        ne.printStackTrace(System.err);
                        throw CustomException;
                } catch (JMSException jmse) {
                        jmse.printStackTrace(System.err);
                        throw CustomException;
                }
             catch CustomException (e) {
               createConnection();
             }
 
        }
 
        public void onException(javax.jms.JMSException jsme) {
                jsme.printStackTrace();
        }
               createConnection();
        }
 
        public synchronized void publishMessage(String Text) {
          // build and send message 
       }
} 

Open in new window

0
 
LVL 26

Accepted Solution

by:
dpearson earned 500 total points
ID: 38862942
No - you don't want to do it like that.

The problem is that if you get an error,  you will end up with an ever growing stack of "createConnection" calls because you are calling yourself in the exception handler.

This means if you run this for long enough (let's say maybe 100,000 messages with 10% of them triggering an error) you'll eventually run out of stack space and the whole process and server will crash.  That's an ugly error as it'll work fine for a long long time in testing before it fails with an "OutOfMemory" exception and the world's biggest stack trace.

A better model is just to use a loop:
createConnection() {
    boolean done = false  ;
    while (!done) {
       try {
                   // set up connection
                   // status as successful.
                  done = true ; // Stop trying to connect
           } catch (Exception ex) {
                 // Perhaps log the exception...
                done = false ; // Not really needed but makes the point
           }
       }
    }
}

Open in new window


Does that make sense?

Doug
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
ArrayIndexOutOfBoundException 9 81
java 8 lambda expresssions exception handling 3 90
hibernate jars 4 30
JAVA API design with micro service cloud in mind 1 16
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question