Link to home
Start Free TrialLog in
Avatar of jkteater
jkteaterFlag 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?
Avatar of mrcoffee365
mrcoffee365
Flag of United States of America image

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

ASKER

How would I make that call?
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
ASKER CERTIFIED SOLUTION
Avatar of mrcoffee365
mrcoffee365
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
How would you address the ErrorWithExceptionDialog class?  How would you set that class up? (The constructor, etc ...)
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