Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

best practices for custom Exceptions

Posted on 2013-02-05
3
297 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 27

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

860 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