【问题标题】:Exceptions works correct but not how its must异常工作正确,但不是必须的
【发布时间】:2014-02-28 10:49:30
【问题描述】:
  1. 方法processExceptions() 应该调用方法BEAN.methodThrowExceptions 并处理异常:

1.1。如果发生异常FileSystemException,则调用BEAN.log方法记录异常并向前抛出

1.2。如果发生异常CharConversionException 或任何其他IOException,只需通过调用方法BEAN.log 记录它

  1. 添加您在 2.1 中转发的异常的类/类型。到processExceptions() 方法签名。

  2. 在方法main() 中处理剩余的异常并记录它。使用try..catch

我尝试了不同的解决方案。它有效,但不是应有的。 throws 在方法中的正确位置是什么。或者也许我根本不应该使用它们?如果我不放置它们,我将无法使用throw。请帮忙,非常感谢您的宝贵时间。

public class Solution {

    public static StatelessBean BEAN = new StatelessBean();

    public static void main(String[] args) {
        try{
            processExceptions();
        }
        catch (CharConversionException e){
            BEAN.log(e);
        }
    }


    public static void processExceptions()throws CharConversionException {
        try{
            BEAN.methodThrowExceptions();
        }
        catch (CharConversionException e){
            BEAN.log(e);
            throw e;
        }
        catch (FileSystemException e){
            BEAN.log(e);
        }
        catch (IOException e){
            BEAN.log(e);
        }
    }

    public static class StatelessBean {
        public void log(Exception exception) {
            System.out.println(exception.getMessage() + ", " + exception.getClass().getSimpleName());
        }

        public void methodThrowExceptions() throws CharConversionException, FileSystemException, IOException {
            int i = (int) (Math.random() * 3);
            if (i == 0)
                throw new CharConversionException();
            if (i == 1)
                throw new FileSystemException("");
            if (i == 2)
                throw new IOException();
        }
    }
}

【问题讨论】:

  • 这看起来像是对需求的正确再现;然而,要求本身被打破:要么重新抛出 日志,从不两者兼而有之。此外,如果FileSystemException extends IOException,那么显式捕获它是多余的,因为捕获块是相同的。
  • 当你说它不能正常工作时,它的确切问题是什么?
  • 你的意思是我不能使用 throws 并登录一个 try-catch 块?
  • @Predict_it 你可以。这只是糟糕的风格。

标签: java exception ioexception throw throws


【解决方案1】:

如果一个方法能够抛出不是 RuntimeException 的异常(直接抛出或调用可以抛出异常的方法),它应该处理异常或声明它抛出异常,以便任何其他方法调用此方法的它会知道它可能会遇到异常,并且可以处理它或声明它抛出(等等)。

由于您正在处理已检查的异常,因此没有干净的方法来避免声明抛出,但是有一个(混乱的)解决方法。您可以将异常包装在 RuntimeException 中并抛出它,当您想要处理它时,您可以从 re.getCause(); 中获取实际的异常;

public class Solution {

    public static StatelessBean BEAN = new StatelessBean();

    public static void main(String[] args) {
        try{
            processExceptions();
        }
        catch (RuntimeException re){
            if (!(re.getCause() instanceof CharConversationException)) {
                //handle the case in which the exception was not CCE and not FSE not IOException
            }
        }
    }


    public static void processExceptions() {
        try{
            BEAN.methodThrowExceptions();
        } catch (CharConversionException cce){
            BEAN.log(e);
            throw new RuntimeException(cce);
        } catch (FileSystemException fse){
            BEAN.log(e);
        } catch (IOException e){
            BEAN.log(e);
        }
    }

    public static class StatelessBean {
        public void log(Exception exception) {
            System.out.println(exception.getMessage() + ", " + exception.getClass().getSimpleName());
        }

        public void methodThrowExceptions() throws CharConversionException, FileSystemException, IOException {
            int i = (int) (Math.random() * 3);
            if (i == 0)
                throw new CharConversionException();
            if (i == 1)
                throw new FileSystemException("");
            if (i == 2)
                throw new IOException();
        }
    }
}

我不确定我是否正确理解了您的问题,这就是您想要的:)

【讨论】:

  • 我不明白 =(,我为什么要使用 runtimeexception
【解决方案2】:

我认为顺序:

在main()方法中处理remaining异常

意味着您不仅应该捕获CharConversionException,还应该通过以下方式捕获所有其他异常:

catch (Exception e)

另外,我想你应该在help.javarush.net 上提问:>

【讨论】:

    猜你喜欢
    • 2010-10-15
    • 1970-01-01
    • 1970-01-01
    • 2017-10-18
    • 2020-08-09
    • 1970-01-01
    • 2012-11-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多