为什么在我的super()调用周围不能使用try块? [英] Why can't I use a try block around my super() call?

查看:83
本文介绍了为什么在我的super()调用周围不能使用try块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,在Java中,构造函数的第一行必须是对super的调用...是隐式调用super()还是显式调用另一个构造函数.我想知道的是,为什么我不能对此进行尝试?

So, in Java, the first line of your constructor HAS to be a call to super... be it implicitly calling super(), or explicitly calling another constructor. What I want to know is, why can't I put a try block around that?

我的具体情况是我有一个用于测试的模拟类.没有默认的构造函数,但是我希望它使测试更易于阅读.我还想将构造函数抛出的异常包装到RuntimeException中.

My specific case is that I have a mock class for a test. There is no default constructor, but I want one to make the tests simpler to read. I also want to wrap the exceptions thrown from the constructor into a RuntimeException.

所以,我想做的实际上是这样的:

So, what I want to do is effectively this:

public class MyClassMock extends MyClass {
    public MyClassMock() {
        try {
            super(0);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    // Mocked methods
}

但是Java抱怨super不是第一句话.

But Java complains that super isn't the first statement.

我的解决方法:

public class MyClassMock extends MyClass {
    public static MyClassMock construct() {
        try {
            return new MyClassMock();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public MyClassMock() throws Exception {
        super(0);
    }

    // Mocked methods
}

这是最好的解决方法吗?为什么Java不让我做前者?

Is this the best workaround? Why doesn't Java let me do the former?

我对为什么"的最佳猜测是Java不想让我让处于潜在不一致状态的构造对象……但是,在进行模拟时,我并不关心这一点.看来我应该能够执行以上操作……或者至少我知道以上内容对我而言是安全的……或者似乎无论如何应该如此.

My best guess as to the "why" is that Java doesn't want to let me have a constructed object in a potentially inconsistent state... however, in doing a mock, I don't care about that. It seems I should be able to do the above... or at least I know that the above is safe for my case... or seems as though it should be anyways.

我将覆盖测试类中使用的所有方法,因此没有使用未初始化变量的风险.

I am overriding any methods I use from the tested class, so there is no risk that I am using uninitialized variables.

推荐答案

不幸的是,编译器无法按照理论原理工作,即使您可能知道在您的情况下它是安全的,但如果他们允许的话,它本来可以在所有情况下都是安全的.

Unfortunately, compilers can't work on theoretical principles, and even though you may know that it is safe in your case, if they allowed it, it would have to be safe for all cases.

换句话说,编译器不仅停止了您的工作,还在停止所有人,包括所有不知道它不安全且需要特殊处理的人.可能还有其他原因,因为如果一种语言知道如何处理,那么所有语言通常都具有处理不安全的方法.

In other words, the compiler isn't stopping just you, it's stopping everyone, including all those that don't know that it is unsafe and needs special handling. There are probably other reasons for this as well, as all languages usually have ways to do unsafe things if one knows how to deal with them.

在C#.NET中有类似的规定,而声明调用基本构造函数的构造函数的唯一方法是:

In C# .NET there are similar provisions, and the only way to declare a constructor that calls a base constructor is this:

public ClassName(...) : base(...)

这样做,基本构造函数将在构造函数的主体之前被调用,并且您不能更改此顺序.

in doing so, the base constructor will be called before the body of the constructor, and you cannot change this order.

这篇关于为什么在我的super()调用周围不能使用try块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆