Java 处理异常的问题

Java 处理异常的问题,java,exception,Java,Exception,我已经创建了自己的异常,但当我尝试使用它时,我收到一条消息,说它无法转换为我的异常 我有一个这样的界面 public interface MyInterface { public OtherClass generate(ClassTwo two, ClassThree three) throws RetryException; } 其他的像这个 public class MyGenerator{ public class generate (ClassTwo two,

我已经创建了自己的异常,但当我尝试使用它时,我收到一条消息,说它无法转换为我的异常

我有一个这样的界面

public interface MyInterface
{   
    public OtherClass generate(ClassTwo two, ClassThree three) throws RetryException;
}
其他的像这个

public class MyGenerator{   
  public class generate (ClassTwo two, ClassThree three){
    try{
    }catch(MyException my)
  }
}
最后是另一个类中的方法

public Object evaluate(String expression, Map values) throws FirstException, RetryException
{
   try{
   }catch (Exception x){
     if(x instanceof FirstException){
       throw new FirstException()
     }
     else{
       RetryException retry= (RetryException)x;
       retry.expression = expression;
       retry.position = position;
       retry.operator = tokens[position];
       retry.operand = 1;
       throw retry; 
     }
  }
}
最后一个方法的try-catch块用于进行数学运算,我想在
RetryException
上捕获一个除零异常

RetryException retry= (RetryException)x; 
这行代码试图将异常强制转换为RetryException。只有当:RetryException适当地扩展了您正在捕获的异常类型(我想是算术异常除以零?)。这个异常实际上是一个RetryException。如果不看你的逻辑,我们不知道这是不是真的

试试看

if (x instanceof RetryException)
在你做这个演员之前。您的代码可能会引发不同类型的异常

public Object evaluate(String expression, Map values) throws FirstException, RetryException
{
   try{
       // Some code that may throw FirstException
       int x = 10/0;
   }catch (ArithmeticException x){  // Handle divide by zero
       RetryException retry= new RetryException();
       retry.setExpression(expression);
       retry.setPosition(position);
       retry.setOperator(tokens[position]);
       retry.setOperand(1);
       throw retry; 
     }
  }
}
最好是有多个捕捉块

try{
//}catch (FirstException e) -- I removed this, as you are only catching it and then directly
                        //     throwing it, which seems uneecessary
}catch (RetryException r){
    //process r
    throw r;
}

如果我误解了你的问题,我会尽力纠正。

由于代码示例非常不完整,我将在这里对发生的情况进行一些大的假设

在求值方法中,由于被零除,您将得到一个算术异常,您希望通过在处理程序中抛出自己的RetryException来处理它。接收到的异常无法强制转换,因为它的类型错误。您应该在求值方法中捕获算术异常,然后创建并抛出新的RetryException

public Object evaluate(String expression, Map values) throws FirstException, RetryException
{
   try{
       // Some code that may throw FirstException
       int x = 10/0;
   }catch (ArithmeticException x){  // Handle divide by zero
       RetryException retry= new RetryException();
       retry.setExpression(expression);
       retry.setPosition(position);
       retry.setOperator(tokens[position]);
       retry.setOperand(1);
       throw retry; 
     }
  }
}
当然,这一切都假设适当的setter方法存在适当的RetryException


FirstException捕获被删除,因为它通过在不需要任何实例时创建新实例来隐藏真正的stacktrace。它已经在方法签名中声明,没有实际的处理。

我修复了代码标记,然后有人在我后面编辑并弄乱了它们……只是一个注释:不要在catch块中使用
if(x instanceof FirstException)
,而是使用多个catch块。显示异常的实际代码和堆栈跟踪。您没有显示任何实际生成异常的代码。
RetryException
是如何定义的?这是一个很好的答案,但Java代码的格式非常错误
setProperty()=x
应该是
setProperty(x)
@samdehaan是的,我只是在重构OP的代码,有点匆忙。谢谢你指出,现在已经修好了。是的,重构很棒,只是不想+1,直到它被纠正。问题是这个
异常
被假定为处理任何类型的
异常
,例如,当它在我的代码中找到一个字母而不是一个数字时,异常遵循这个树
异常->MyFirstException->RetryException
,所以,没有人能为我找到解决方案?归根结底是这样的:您试图将异常强制转换为RetryException(
(RetryException)x
)。异常不一定是RetryException,因此这是失败的。我们提供了一些解决方案,但您没有接受它们,也没有提供它们不起作用的原因。因此,我认为唯一的解决方案是将其扩展到算术异常