当前位置: 首页 > 知识库问答 >
问题:

在访问者模式中抛出自己定义的异常Java

顾炎彬
2023-03-14

我必须使用访问者设计模式编写一个应用程序,如(https://www.baeldung.com/java-visitor-pattern),一个示例元素如下所示:

public class Call implements Stmt {
    int hash_id;
    private String id;
    public Call(String id )   { this.id = id;}


    public void accept(Visitor v) {
        System.out.println("call.accept");
        v.visit( this);
    }

    public String getId () {  return id;}
    ....}

vistor界面看起来像下面的代码,我可能不会更改界面代码。

public interface Visitor {
    ....... 

    void visit(Repeat repeat);

    void visit(Subroutine subroutine);

    void visit(Call call);
}

在Visitor(AbstractVisitor类)的实现中,如果hashtable子befehle中还没有调用类的id,我必须抛出一个“UndefinedSubroutineException”。带有try-catch statmenets的现有代码从不将异常发送到我的测试用例。。。。

public abstract class AbstractVisitor implements Visitor {
.....
 private Hashtable<String, Integer> SubBefehle = new Hashtable< String, Integer>();

@Override
public void visit(Call call)  {
    ///  implementation work :

       try {

           IsFunctionNameExisting(call.getId());
       }
       catch (UndefinedSubroutineException e){
          // throw new UndefinedSubroutineException("");
       }
}

public void IsFunctionNameExisting(String fctname) throws UndefinedSubroutineException {
    boolean IsFound = false;

    if (SubBefehle.containsKey(fctname)) {
        System.out.println("Abstract visitor :  function name found -> PASS  ");
        IsFound = true;
    }
    else {
        System.out.println("Abstract visitor : function name not yet found -> EXCEPTION ");
         throw new UndefinedSubroutineException(" function name not found in function name list !");
    }

如果调用输入(call.getId())为false。

@Override
    public void visit(Call call) throws UndefinedSubroutineException  {
        ///  implementation work :
               IsFunctionNameExisting(call.getId());
           }

但如果我这样尝试,则会出现“turtle.logo.AbstractVisitor”中的“visit(Call)”错误与“turtle.stmt.Visitor”中的“visit(Call)”冲突;重写的方法不会抛出“turtle.logo.undefinedsubroutineeexception”,因为我与我的访问者界面冲突。但如果我尝试在以下控制函数中抛出“UndefinedSubroutineException”:

there is a different Error in my Testcase: `"Exception 'turtle.logo.UndefinedSubroutineException' is never thrown in the corresponding try block"`.

因此,我不能在我的visit类中抛出Exeption,因为它与我的visitor接口冲突,也不能在我的“控制类”(IsFunctionNameExisting)中抛出Exeption,因为我的测试用例中出现了错误,wich声称我永远不会抛出它。

PS:我不能改变测试用例,我希望有人能帮助我解决这个不同的问题。

共有1个答案

归翔
2023-03-14

>

  • 你不应该把访问的签名扔掉,然后用这种方法抓住它

    public void visit(Call call) throws UndefinedSubroutineException {
        ///  implementation work :
    
           IsFunctionNameExisting(call.getId());
    }
    

    为什么您的metode IsFunctionNameExtics(...)不返回布尔值?重命名方法或返回布尔值,如下所示:

    public boolean IsFunctionNameExisting(String fctname) throws UndefinedSubroutineException {
    
    if (SubBefehle.containsKey(fctname)) {
        System.out.println("Abstract visitor :  function name found -> PASS  ");
        return true;
    }
    
    System.out.println("Abstract visitor : function name not yet found -> EXCEPTION ");
    throw new UndefinedSubroutineException(" function name not found in function name list !");
    
    }
    

  •  类似资料:
    • 我是一名Java编程新手(实际上已经在学习),我对如何处理不同的消息有些怀疑。 我的目标是将这些不同的消息包含在同一个类(CustomExcpse类)中,以避免在从其他类抛出新CustomExceptions的每个方法上一遍又一遍地编写相同的字符串。 到目前为止,我编码: > 一个自定义异常类,它从异常扩展而来,具有不同的消息(在示例中只有两个,但还有更多)作为Strings包含,当然还有构造函数

    • "CATCH"应该严格地在"扔"之后叫吗?" 例1: 错误: 找不到方法“接收器”:没有方法缓存,也没有^在/tmp/739536251/main块中查找_方法。pl6第11行 例2: 无误

    • 本文向大家介绍Django 解决开发自定义抛出异常的问题,包括了Django 解决开发自定义抛出异常的问题的使用技巧和注意事项,需要的朋友参考一下 在开发过程中,针对用户输入的不合法信息,我们应该在后端进行数据验证,并抛出相关的异常传递到前端来提示用户。 可是如何进行自定义抛出异常信息呢?通常处理方法有三种,我将依次介绍这三种方法。 第一种方法: 这种方法最为简单,只需要创建一个字典对象,通过re

    • 假设我有一套接受访问者(访问者模式)的类,但是由于这些类或特定访问者的性质,对它们执行工作可能会引发一个检查异常。 访客接受界面: 访客界面: 以及哺乳动物的实现: 我们假设是狗 现在假设我的访客是: 我将结果打印到标准输入输出: 然而,上面的句子在语法上是不正确的,因为它是可附加的。append(String)抛出。我不能声明每个访问方法的抛出,因为它没有在访问者界面中声明。 我考虑过的解决方案

    • 本文向大家介绍Java抛出异常与自定义异常类应用示例,包括了Java抛出异常与自定义异常类应用示例的使用技巧和注意事项,需要的朋友参考一下 本文实例讲述了Java抛出异常与自定义异常类。分享给大家供大家参考,具体如下: 异常处理常用方法: 常用的异常处理方法有: 一、try()catch() 语句 二、throw / throws 语句 三、自定义异常类 用途: 众所周知,当程序运行过程中,如果遇

    • 定义自己的索引模式 加载到 Elasticsearch 的每组数据都有一个索引模式(Index Pattern)。 在上一节中,为莎士比亚数据集创建了名为 shakespeare 的索引,为 accounts 数据集创建了名为 bank 的索引。一个 _索引模式_ 是可以匹配多个索引的带可选通配符的字符串。例如一般在通用日志记录中,一个典型的索引名称一般包含类似 YYYY.MM.DD 格式的日期信