Solved

best practices for custom Exceptions

Posted on 2013-02-05
3
300 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
[X]
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
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 28

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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
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 …
Suggested Courses

630 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