Solved

best practices for custom Exceptions

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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
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…
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…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

756 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