有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

Java捕获对象错误

下面的代码是方法头和方法体,但我得到了以下错误:no exception of type object can be thrown an exception type must be a subclass of Throwable。我正在尝试执行这段代码:catch(Object object)

public void method15665(Class435 class435, int i) {
    do {
        try {
            try {
                byte[] is
                        = new byte[(int) class435.method7563(1085678935)];
                int i_3_;
                for (int i_4_ = 0; i_4_ < is.length; i_4_ += i_3_) {
                    i_3_ = class435.method7564(is, i_4_, is.length - i_4_,
                            (byte) -10);
                    if (i_3_ == -1)
                        throw new EOFException();
                }
                Class224_Sub8 class224_sub8 = new Class224_Sub8(is);
                if ((class224_sub8.aByteArray8535.length
                        - class224_sub8.anInt8536 * 475822179)
                        < 1) {
                    try {
                        class435.method7572(-1683167102);
                    } catch (Exception exception) {
            /* empty */
                    }
                    break;
                }
                int i_5_ = class224_sub8.method13859((short) -7287);
                if (i_5_ < 0 || i_5_ > 1) {
                    try {
                        class435.method7572(-1683167102);
                    } catch (Exception exception) {
            /* empty */
                    }
                    break;
                }
                if ((class224_sub8.aByteArray8535.length
                        - class224_sub8.anInt8536 * 475822179)
                        < 2) {
                    try {
                        class435.method7572(-1683167102);
                    } catch (Exception exception) {
            /* empty */
                    }
                    break;
                }
                int i_6_ = class224_sub8.method13737(2071056893);
                if ((class224_sub8.aByteArray8535.length
                        - 475822179 * class224_sub8.anInt8536)
                        < 6 * i_6_) {
                    try {
                        class435.method7572(-1683167102);
                    } catch (Exception exception) {
            /* empty */
                    }
                    break;
                }
                for (int i_7_ = 0; i_7_ < i_6_; i_7_++) {
                    Class323 class323
                            = Class399.aClass195_Sub2_Sub1_5932
                            .method14614(class224_sub8, -2141543778);
                    if ((Class255.aClass255_3016
                            == (((Class173_Sub1) this).aClass255Array9960
                            [class323.anInt5015 * 1568411443]))
                            && (Class399.aClass195_Sub2_Sub1_5932.method14624
                            (class323.anInt5015 * 1568411443, 82620551)
                            .aClass350_2171.method6687
                                    (-1035085164).aClass5162.isAssignableFrom
                                    (class323.anObject5014.getClass())))
                        anInterface50_2149.method298((class323.anInt5015
                                * 1568411443),
                                class323.anObject5014,
                                -1250481088);
                }
            } catch (Exception exception) {
                try {
                    class435.method7572(-1683167102);
                } catch (Exception exception_8_) {
                    exception = exception_8_;
                }
                break;
            }
            try {
                class435.method7572(-1683167102);
            } catch (Exception exception) {
        /* empty */
            }
        } catch (Object object) {
            try {
                class435.method7572(-1683167102);
            } catch (Exception exception) {
        /* empty */
            }
            throw object;
        }
    } while (false);
}

有人知道怎么解决这个问题吗?非常感谢


共 (3) 个答案

  1. # 1 楼答案

    你只能捕捉可以抛出的(IS-A ^{)。因此,当您试图捕获一个对象(因为它不扩展Throwable)时,编译器会发出抱怨

    catch (Object o) // Error: Object IS-NOT Throwable
    

    Throwable被所有类型的ErrorException继承。但是,我们通常不会捕获Error,因为程序几乎总是无法从中恢复,例如,OutOfMemoryError。因此,不建议使用catch (Throwable t)

    当使用catch (Exception e)时,对于运行期间可能抛出的任何异常(选中的未选中的),基本上都有一个全包)。使用或不使用泛型catch通常取决于try块试图做什么。例如,在读取文件时,您希望以不同于EOFException的方式处理和响应FileNotFoundException

  2. # 2 楼答案

    所有异常和错误都会扩展Throwable,只有那些异常和错误才能被抛出和捕获

    你能行

     try{
        throw new Exception();
      }catch(Exception e){
        // something here
      }catch(Throwable t){
        // something here
      }
    

    在编写多个catch块时,请记住以下几点

    • 不能在超类类型之后编写子类类型。i、 e.如果你在catch(Exception e){}之后写catch(RuntimeException rt){},那么你会得到编译器错误,因为它已经被捕获
  3. # 3 楼答案

    替换

    } catch (Object object) {
    

    } catch (Throwable object) {
    

    实际上,您不想捕获Throwable,但可能是ExceptionRuntimeException或更具体的类