troubleshooting Question

Calling a Class method from within a method in a separate class

Avatar of jkteater
jkteaterFlag for United States of America asked on
Java
6 Comments1 Solution329 ViewsLast Modified:
I have found that I am using the same method in 4 different classes.

So I want it would be best to create a class for the method.
That way the other classes can just call the class.

Here is the method:

 
  //////////////////////////////////////////////////////////////////////////
   //                      errorDialogWithStackTrace()                     //
   //////////////////////////////////////////////////////////////////////////
   public static void errorDialogWithStackTrace(final String msg, final Throwable ex) {
      Display.getDefault().asyncExec(new Runnable() {
         public void run() {
            StringWriter sw = new StringWriter();
            PrintWriter pw = new PrintWriter(sw);
            ex.printStackTrace(pw);
            final String trace = sw.toString();
            ArrayList<Status> childStatuses = new ArrayList<Status>();
            for (String line : trace.split(System.getProperty("line.separator"))) {
               childStatuses.add(new Status(IStatus.ERROR, Activator.PLUGIN_ID, line));
           }
            MultiStatus ms = new MultiStatus(Activator.PLUGIN_ID, IStatus.ERROR,
                  (IStatus[]) childStatuses.toArray(new Status[] {}), 
                  ex.getLocalizedMessage(), ex);
            ErrorDialog err = new ErrorDialog( new Shell(), "Aplot Exception Caught", msg, ms, IStatus.ERROR );
            err.open();
         }
      });
   }

This is how the method called.

catch (final Exception ex) {
         String msg = "Error getting SavePDF reply file";
         errorDialogWithStackTrace(msg, ex);
          throw ex;
} // end catch

This is my first attempt at the class.

public class ErrorWithExceptionDialog {
   
   public ErrorWithExceptionDialog(String message, Throwable ex) {           
   }        
       
   public void startOperation(final String msg, final Throwable ex) {
      Display.getDefault().asyncExec(new Runnable() {
         public void run() {
            System.out.println("Got In\n");
            StringWriter sw = new StringWriter();
            PrintWriter pw = new PrintWriter(sw);
            ex.printStackTrace(pw);
            final String trace = sw.toString();
            ArrayList<Status> childStatuses = new ArrayList<Status>();
            for (String line : trace.split(System.getProperty("line.separator"))) {
               childStatuses.add(new Status(IStatus.ERROR, Activator.PLUGIN_ID, line));
            }
            MultiStatus ms = new MultiStatus(Activator.PLUGIN_ID, IStatus.ERROR,
               (IStatus[]) childStatuses.toArray(new Status[] {}), ex.getLocalizedMessage(), ex);
            ErrorDialog err = new ErrorDialog( new Shell(), "Aplot Exception Caught", msg, ms, IStatus.ERROR );
            err.open();
         }
      });
   }                                                          
}

I was trying to call it.

catch (final Exception ex) {
         String msg = "Error getting SavePDF reply file";
         ErrorWithExceptionDialog ewed = new ErrorWithExceptionDialog(msg, ex);
         throw ex;
 } // end catch

Does not seem to be working.  

I am not sure if I am not calling the class correctly?
I am not sure if I set up the new class correctly?
Instead of using startOperation, should I have set it up more like a normal class?
ASKER CERTIFIED SOLUTION
Join our community to see this answer!
Unlock 1 Answer and 6 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 6 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros