无法处理隐式超级构造函数抛出的异常类型 [英] Cannot handle exception type thrown by implicit super constructor

查看:915
本文介绍了无法处理隐式超级构造函数抛出的异常类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Cage类:

public class Cage<T extends Animal> {

    Cage(int capacity) throws CageException {
        if (capacity > 0) {
            this.capacity = capacity;
            this.arrayOfAnimals = (T[]) new Animal[capacity];                                                       
        }

        else {
            throw new CageException("Cage capacity must be integer greater than zero");
        }
    }
}

我试图实例化一个Cage的另一个主要方法的对象:

I am trying to instantiate an object of Cage in another class main method:

private Cage<Animal> animalCage = new Cage<Animal>(4);

我收到错误:默认构造函数无法处理由隐式超级构造函数抛出的异常类型CageException。必须定义一个显式的构造函数。有任何想法吗? :o(

I get the error: "Default constructor cannot handle exception type CageException thrown by implicit super constructor. Must define an explicit constructor." Any ideas? :o(

推荐答案

这意味着在你的另一个类的构造函数中你正在创建Cage类,但是那个构造函数不是'正确处理异常。

This means that in the constructor of your other class you are creating the Cage class, but that constructor isn't properly handling the exception.

因此,要么在其他构造函数中创建 Cage 类时捕获异常,或使构造函数抛出 CageException

So either just catch the exception when you create the Cage class in the other constructor, or make the constructor throws CageException.

这篇关于无法处理隐式超级构造函数抛出的异常类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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