【问题标题】:Try-Catch-Throw in the same Java classTry-Catch-Throw 在同一个 Java 类中
【发布时间】:2011-01-24 14:02:30
【问题描述】:

是否可以在运行 try-catch 块的当前类中捕获方法?例如:

  public static void arrayOutOfBoundsException(){
      System.out.println("Array out of bounds");
  }

    .....

  public static void doingSomething(){
    try
    {
       if(something[i] >= something_else);
    }
    catch (arrayOutOfBoundsException e)
    {
       System.out.println("Method Halted!, continuing doing the next thing");
    }
  }

如果这是可能调用 catch 方法的正确方法是什么?

如果这是不可能,谁能指出我正确的方向,如何阻止异常停止我在 Java 中的程序执行而无需创建任何包中的新类,或修复产生 ArrayOutOfBoundsException 错误的代码。

提前致谢,

Java 新手

【问题讨论】:

  • catch 子句查找异常 class 而不是 method。 因此参数中的 e 是一个 Exception 对象(或 )

标签: java arrays exception-handling error-handling try-catch


【解决方案1】:

不清楚“是否可以捕获方法”是什么意思。在您的情况下,您应该将代码编写为:

   if(i < something.length && something[i] >= something_else);

不需要ArrayOutOfBoundsException

【讨论】:

    【解决方案2】:

    我认为您的意思是让 arrayOutOfBoundsException 方法抛出导致的异常......如果我理解正确:

    public static void arrayOutOfBoundsExceptionMethod() throws ArrayIndexOutOfBoundsException  {
          \\...
          \\ code that throws the exception
      }
    
        .....
    
      public static void doingSomething(){
        try
        {
           if(something[i] >= something_else);
           arrayOutOfBoundsExceptionMethod();
        }
        catch (ArrayIndexOutOfBoundsException e)
        {
           System.out.println("Method Halted!, continuing doing the next thing");
        }
      }
    

    这就是你要问的吗?

    【讨论】:

    • 没有 ArrayOutOfBoundsException :) 但是有 ArrayIndexOutOfBoundsException,这是被抛出的。
    • 哦,拜托,我只是复制了他的代码...我什至没有注意拼写:P
    【解决方案3】:

    您可以使用 try-catch 块捕获异常。不确定您是否可以使用此方法捕获方法。

    public static void myMethod(){
        try {
            // check for some given condition
        }
        catch (conditionNotSatisfied e) {
            System.out.println("Condition was not satisfied, you tried to do something which is not allowed");
        }
    }
    

    catch() 中的“e”是用于引用异常的引用。 Exception 指的是当它希望检查您的给定条件并返回有关条件未满足的警告或错误时。

    【讨论】:

    • 感谢您分享您的知识,伙计,我当然会记住您的回复,但我现在要接受 Adam 的回复。放轻松
    • 根本不是问题,我认为他已经很好地解决了这个问题:-)
    【解决方案4】:

    您想要做的是处理Exception

    public static void doingSomething(){
        try {
            if (something[i] >= something_else) { ... }
        } catch (ArrayIndexOutOfBoundsException e) {
            System.out.println("Method Halted!, continuing doing the next thing");
        }
    }
    

    这就是你所需要的。没有额外的类,没有额外的方法。

    异常是一种可以“抛出”的特殊类型(您可以使用 throw 关键字自己抛出异常,或者如果您尝试访问数组索引,Java 可能会为您抛出异常)不存在或尝试对null 执行某些操作)。抛出的异常将“解开”您的调用堆栈(从每个函数调用中“转义”),直到您的程序最终终止。除非你 catch 它,这正是上面的语法所做的。

    因此,如果您正在编写一个函数a(),该函数调用了一个函数b(),该函数调用了一个函数c(),并且c() 引发了异常,但该异常未在b()c() 中捕获,你仍然可以在a() 中看到它:

    void a() {
        try {
            b();
        catch (SomeExceptionClass e) {
            // Handle
        }
    }
    

    也就是说,如果有可能从一开始就防止抛出异常,那通常是一个更好的主意。在您的特定情况下,这是可能的,因为 Java 中的所有数组都知道它们自己的长度:

    public static void doingSomething(){
        if (i >= something.length) {
            System.out.println("Method Halted!, continuing doing the next thing");
        } else {
            if (something[i] >= something_else) { ... }
        }
    }
    

    【讨论】:

    • 非常感谢您的回答,我无法修改导致错误的代码,因为我正在评估它是否写对了。
    • 我想我明白你的意思了。我在中间添加了一些内容来解释如何仅通过将函数调用本身包装在 try/catch 块中来捕获异常。
    • 太好了,这似乎工作得很好。非常感谢朋友。你的解释不仅对解决问题非常有用,而且对理解它的实际工作原理非常有用。
    【解决方案5】:

    我试图理解你在问什么,你的术语在某些地方是错误的 :) 如果你尝试设置一个超出范围的索引,无论如何你都会得到一个内置的异常,ArrayIndexOutOfBoundsException。您可以捕获它并在方法中使用它,或者在方法的标头中使用class XX throws ArrayIndexOutOfBoundsException 声明将其抛出,以便调用方法可以捕获它(它不需要捕获ArrayIndexOutOfBoundsException,因为它是@987654325 @ -- 即,如果它未被捕获,则不会发生任何事情,它是未经检查的。

    有关详细信息,请参阅此 wiki 文章:http://en.wikibooks.org/wiki/Java_Programming/Throwing_and_Catching_Exceptions

    【讨论】:

      【解决方案6】:

      嗯..我觉得你有点困惑。您不会使用 try catch 块捕获方法。你捕捉到异常。异常更像是事物。

      仅此一项(注意 ArrayOutOfBoundsException 的大写“A”)将阻止您的程序终止,即使抛出该异常也是如此。您无需声明“arrayOutOfBoundsException()”方法。

      public static void doingSomething(){
          try
          {
             if(something[i] >= something_else);
          }
          catch (ArrayOutOfBoundsException e)
          {
             System.out.println("Method Halted!, continuing doing the next thing");
          }
        }
      

      注意到小“e”了吗?这是您可以用来引用此异常的参考。所以,你可以使用这个小局部变量来询问这个异常。例如,如果要打印异常发生的位置,可以执行 e.printStackTrace();

      【讨论】:

      • 对,我实际上尝试过,但 netbeans 告诉我我需要在包中创建一个类“ArrayOutOfBoundsException”,我真的不想创建一个类,有什么想法吗?非常感谢您的回答,您很友善
      • 名称是 'ArrayIndexOutOfBoundsException' - 你只是缺少了 'Index' 部分。
      猜你喜欢
      • 2010-12-14
      • 2011-04-17
      • 1970-01-01
      • 2013-10-07
      • 1970-01-01
      • 2015-07-07
      • 1970-01-01
      • 1970-01-01
      • 2011-12-25
      相关资源
      最近更新 更多