Solved

best practices for custom Exceptions

Posted on 2013-02-05
3
293 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 20

Expert Comment

by:Amitkumar Panchal
Comment Utility
0
 

Author Comment

by:dpalyca755
Comment Utility
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
Comment Utility
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

763 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now