Java 试捕解释

Java 试捕解释,java,Java,我认为try和catch块在捕获异常时停止,然后继续执行catch块。然而,情况并非如此,它将继续运行整个try块 顺便说一下,这是一棵树,参考了3个节点:左、中、右 捕捉到的异常是NullPointerException,其中动物名称重复两次。例如,“Python”出现在左侧节点,然后“Python”再次出现在中间节点 可能是因为在另一个类中捕获了异常?我不太确定 因此,用户输入的“AC”和异常会在OrganimTree.addAnimalChild()中捕获 addAnimalChild()

我认为try和catch块在捕获异常时停止,然后继续执行catch块。然而,情况并非如此,它将继续运行整个try块

顺便说一下,这是一棵树,参考了3个节点:左、中、右

捕捉到的异常是
NullPointerException
,其中动物名称重复两次。例如,“Python”出现在左侧节点,然后“Python”再次出现在中间节点

可能是因为在另一个类中捕获了异常?我不太确定

因此,用户输入的“AC”和异常会在
OrganimTree.addAnimalChild()中捕获

addAnimalChild()
然后重定向到另一个类

addAnimalChild方法

public void addAnimalChild(String name, boolean isHerbivore, boolean isCarnivore) throws
            IllegalArgumentException,  Exceptions.PositionNotAvailableException
    {
        try {
            cursor.addPrey(new OrganismNode(name, isHerbivore, isCarnivore));
        }
        catch(Exceptions.DietMismatchException | Exceptions.IsPlantException e)
        {
            e.getMessage();
        }
    }
 public void addPrey(OrganismNode preyNode) throws Exceptions.PositionNotAvailableException, Exceptions.IsPlantException, Exceptions.DietMismatchException
    {
            if (this.isGetAllNodes())
                throw new Exceptions.PositionNotAvailableException();
            if (this.isPlant()) {
                throw new Exceptions.IsPlantException();
            }
            if(((this.isHerbivore() && !this.isCarnivore()) && preyNode.isCarnivore()) || ((this.isCarnivore() && !this.isHerbivore()) && preyNode.isPlant()) )
                throw new Exceptions.DietMismatchException();
            if(this.getLeft() == null)
            {
                this.setLeft(preyNode);
                this.getLeft().setLeafNode(true);
                if(!preyNode.isCarnivore() && !preyNode.isHerbivore() && !preyNode.isOmnivore())
                    this.getLeft().setPlant(true);
                this.setLeafNode(false);
            }
            else if(this.getMiddle() == null)
            {
                try {
                    this.setMiddle(preyNode);
                    if (this.getMiddle().getName().equals(this.getLeft().getName())) {
                        this.setMiddle(null);
                            throw new IllegalArgumentException();
                    }
                    this.getMiddle().setLeafNode(true);
                    if (!preyNode.isCarnivore() && !preyNode.isHerbivore() && !preyNode.isOmnivore())
                        this.getMiddle().setPlant(true);
                    this.setLeafNode(false);
                }
                catch (IllegalArgumentException e) {
                    System.out.println("This prey already exists for this predator");
                }
            }
            else if(this.getRight() == null)
            {
                try {
                    this.setRight(preyNode);
                    if (this.getLeft().getName().equals(this.getRight().getName()) || this.getMiddle().getName().equals(this.getRight().getName())) {
                        this.setRight(null);
                            throw new IllegalArgumentException();
                    }
                    this.getRight().setLeafNode(true);
                    if (!preyNode.isCarnivore() && !preyNode.isHerbivore() && !preyNode.isOmnivore())
                        this.getRight().setPlant(true);
                    this.setLeafNode(false);
                }
                catch (IllegalArgumentException e) {
                    System.out.println("Animal already exists");
                }
            }
    }
然后它重定向到另一个类中的addPrey()

addPrey()方法

public void addAnimalChild(String name, boolean isHerbivore, boolean isCarnivore) throws
            IllegalArgumentException,  Exceptions.PositionNotAvailableException
    {
        try {
            cursor.addPrey(new OrganismNode(name, isHerbivore, isCarnivore));
        }
        catch(Exceptions.DietMismatchException | Exceptions.IsPlantException e)
        {
            e.getMessage();
        }
    }
 public void addPrey(OrganismNode preyNode) throws Exceptions.PositionNotAvailableException, Exceptions.IsPlantException, Exceptions.DietMismatchException
    {
            if (this.isGetAllNodes())
                throw new Exceptions.PositionNotAvailableException();
            if (this.isPlant()) {
                throw new Exceptions.IsPlantException();
            }
            if(((this.isHerbivore() && !this.isCarnivore()) && preyNode.isCarnivore()) || ((this.isCarnivore() && !this.isHerbivore()) && preyNode.isPlant()) )
                throw new Exceptions.DietMismatchException();
            if(this.getLeft() == null)
            {
                this.setLeft(preyNode);
                this.getLeft().setLeafNode(true);
                if(!preyNode.isCarnivore() && !preyNode.isHerbivore() && !preyNode.isOmnivore())
                    this.getLeft().setPlant(true);
                this.setLeafNode(false);
            }
            else if(this.getMiddle() == null)
            {
                try {
                    this.setMiddle(preyNode);
                    if (this.getMiddle().getName().equals(this.getLeft().getName())) {
                        this.setMiddle(null);
                            throw new IllegalArgumentException();
                    }
                    this.getMiddle().setLeafNode(true);
                    if (!preyNode.isCarnivore() && !preyNode.isHerbivore() && !preyNode.isOmnivore())
                        this.getMiddle().setPlant(true);
                    this.setLeafNode(false);
                }
                catch (IllegalArgumentException e) {
                    System.out.println("This prey already exists for this predator");
                }
            }
            else if(this.getRight() == null)
            {
                try {
                    this.setRight(preyNode);
                    if (this.getLeft().getName().equals(this.getRight().getName()) || this.getMiddle().getName().equals(this.getRight().getName())) {
                        this.setRight(null);
                            throw new IllegalArgumentException();
                    }
                    this.getRight().setLeafNode(true);
                    if (!preyNode.isCarnivore() && !preyNode.isHerbivore() && !preyNode.isOmnivore())
                        this.getRight().setPlant(true);
                    this.setLeafNode(false);
                }
                catch (IllegalArgumentException e) {
                    System.out.println("Animal already exists");
                }
            }
    }
它继续打印System.out.println(),即使异常确实被捕获

我刚刚想到了一件事,try块在完成catch块后是否继续运行? 它通过try块,捕获异常,通过catch块,然后继续执行try块在什么位置停止

如果你们需要我的话,我可以进一步澄清


如果这太长,很抱歉。

您的任何代码都无法捕获
NullPointerException
。您只捕获了其他异常类。

您在谈论什么System.out.println()?您有几个,包括catch块中的至少一个。如果一个异常正在底层方法中处理而没有被抛出,那么它将不会从处理该异常的方法中逃逸出来,因此无法达到您的最高级别try catch您的理解似乎过于复杂。在某个时刻,异常会被抛出—通过显式的
throw
语句或隐式操作(例如尝试取消对空引用的引用)抛出。在这一点上,将在堆栈中向后搜索该特定异常类的异常处理程序。在该处理程序中继续执行没有任何有用的功能。它从异常中获取消息文本,然后对该文本不做任何处理。您应该发布stacktrace-这将有助于解决问题