Avatar of jkteater
jkteater
Flag for United States of America asked on

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

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();
         }
      });
   }

Open in new window


This is how the method called.

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

Open in new window


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();
         }
      });
   }                                                          
}

Open in new window


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

Open in new window


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?
Java

Avatar of undefined
Last Comment
CEHJ

8/22/2022 - Mon
mrcoffee365

Where's the part where you call the startOperation method?
jkteater

ASKER
How would I make that call?
CEHJ

It's really a utility method so you might as well make the method static and get rid of the ctor:

 String msg = "Error getting SavePDF reply file";
ErrorWithExceptionDialog.ErrorWithExceptionDialog(msg, ex);

Open in new window


But change the name of the method
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
ASKER CERTIFIED SOLUTION
mrcoffee365

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
jkteater

ASKER
How would you address the ErrorWithExceptionDialog class?  How would you set that class up? (The constructor, etc ...)
CEHJ

That was a an error on my part. What i meant was
String msg = "Error getting SavePDF reply file";
ErrorWithExceptionDialog.startOperation(msg, ex);

Open in new window