0

Im trying to return a JOptionePane message dialog for each one of the possible throws on my method:

public void add_note(String note) throws FileNotFoundException, IOException, InvalidFormatException{
    ... content ...
}

Is there any way to do this?

Barranka
  • 20,547
  • 13
  • 65
  • 83
Cristian
  • 199
  • 2
  • 13

6 Answers6

1

You could try something like :

public void add_note(String note) throws FileNotFoundException, IOException, InvalidFormatException
{
    try
    {
          ...content...
    }
    catch(FileNotFoundException fnfEx)
    {
       throw new FileNotFoundException("File was not found");
    }
    catch(IOException ioEx)
    {
       throw new FileNotFoundException("I/O exception");
    }
    catch(InvalidFormatException invEx)
    {
       throw new FileNotFoundException("Invalid format errror");
    }
}

Where you put the message you want in the new exceptions and you print the exception message in the JOptionPane.

Simon
  • 1,605
  • 13
  • 22
0

Using a Try-Catch you can catch any exception and return something when an exception occurs. You should do this for all of your cases.

   public void add_note(String note){

       try {
           //code
       } catch (FileNotFoundException e) {
           //return something
       }
    }
Majid Laissi
  • 19,188
  • 19
  • 68
  • 105
Arash Saidi
  • 2,228
  • 20
  • 36
0

wrap your code inside try catch. Inside catch block for each exception type throw the message specific to each exception

M Sach
  • 33,416
  • 76
  • 221
  • 314
0

Instead of throwing exceptions, handle each individually in your method:

public JOptionPane add_note(String note) {
    try {
        ...
    } catch (FileNotFoundException fnfe) {
        return ...;
    } catch (IOException ioe) {
        return ...;
    } catch (InvalidFormatException ife) {
        return ...;
    }
}
bstar55
  • 3,542
  • 3
  • 20
  • 24
0

I'll suggest you an alternative approach, as no one mentioned it. I'd use AOP to catch those exceptions and show to the end user. You'll write a simple aspect, and dont mess your code with try and catch blocks.

Here is an example of such aspect

@Aspect
public class ErrorInterceptor{
@AfterThrowing(pointcut = "execution(* com.mycompany.package..* (..))", throwing = "exception")
public void errorInterceptor(Exception exception) {
    if (logger.isDebugEnabled()) {
        logger.debug("Error Message Interceptor started");
    }

    // DO SOMETHING HERE WITH EXCEPTION
    logger.debug( exception.getCause().getMessage());


    if (logger.isDebugEnabled()) {
        logger.debug("Error Message Interceptor finished.");
    }
}
}

If you don't know what Aspect Oriented Programming is definitely go check it out, this is very powerfull concept (just like OOP), spend some time to learn it.

vach
  • 10,571
  • 12
  • 68
  • 106
0

If you want to show a dialog with the JOptionPane.showMessageDialog do as follows:

public void add_note(String note){

   try {
       //code
   } catch (FileNotFoundException | IOException | InvalidFormatException e) {
       JOptionPane.showMessageDialog(frame, e.getMessage(), "Title", JOptionPane.ERROR_MESSAGE);
       //manage the exception here
   }
}
Nicola
  • 395
  • 2
  • 13