【问题标题】:How to specify a message on a method "throws" in Java?如何在 Java 中的“抛出”方法上指定消息?
【发布时间】:2014-06-06 15:44:46
【问题描述】:

我正在尝试为我的方法中的每个可能抛出返回一个 JOptionePane 消息对话框:

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

有什么办法吗?

【问题讨论】:

  • 如果你想为每个实例返回一些值,你应该使用 Try-Catch

标签: java exception throws


【解决方案1】:

你可以试试这样的:

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

您将所需消息放在新异常中的位置,并在 JOptionPane 中打印异常消息。

【讨论】:

    【解决方案2】:

    将您的代码包装在 try catch 中。每个异常类型的 catch 块内部都会抛出特定于每个异常的消息

    【讨论】:

      【解决方案3】:

      使用 Try-Catch 可以捕获任何异常并在异常发生时返回一些内容。您应该为所有案例执行此操作。

         public void add_note(String note){
      
             try {
                 //code
             } catch (FileNotFoundException e) {
                 //return something
             }
          }
      

      【讨论】:

        【解决方案4】:

        不要抛出异常,而是在您的方法中单独处理每个异常:

        public JOptionPane add_note(String note) {
            try {
                ...
            } catch (FileNotFoundException fnfe) {
                return ...;
            } catch (IOException ioe) {
                return ...;
            } catch (InvalidFormatException ife) {
                return ...;
            }
        }
        

        【讨论】:

          【解决方案5】:

          我会建议你另一种方法,因为没有人提到它。 我会使用 AOP 来捕获这些异常并显示给最终用户。你将编写一个简单的切面,不要用 try 和 catch 块弄乱你的代码。

          这是一个这样的方面的例子

          @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.");
              }
          }
          }
          

          如果你不知道什么是面向方面编程,一定要去看看,这是一个非常强大的概念(就像 OOP 一样),花点时间学习一下吧。

          【讨论】:

            【解决方案6】:

            如果您想使用 JOptionPane.showMessageDialog 显示对话框,请执行以下操作:

            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
               }
            }
            

            【讨论】:

              猜你喜欢
              • 2020-09-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2014-04-17
              • 1970-01-01
              • 2023-03-05
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多