使用java中的接口和类处理异常 [英] Handling exceptions with interfaces and classes in java

查看:206
本文介绍了使用java中的接口和类处理异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以画出下面的情景。我有两套类和接口。 ClassA ClassB ClassAInterface ClassBInterface ClassA 在其几种方法中使用 ClassB 。这些来自ClassB的方法有时会抛出异常。

So picture the following scenario. I have two sets of classes and interfaces. ClassA, ClassB, ClassAInterface, and ClassBInterface. ClassA uses ClassB in several of its methods. These methods from ClassB occasionally throw exceptions.

public class ClassA implements ClassAInterface {
   public void example() {
      ClassB test = new ClassB();
      ClassB.exampleTest();

      //more code using ClassB 
   }
}

public class ClassB implements ClassBInterface {
   public void exampleTest() throws ExampleException() {
      boolean tru = false;
      if (tru) {
         throw new ExampleException();
      }
   }
}

我有一个未处理的异常类型ExampleException()错误在 ClassA.example()方法。为什么会这样呢?我想要 ClassB 方法在特定情况下抛出异常,并表示它有时会引发异常使用 throws ExampleException(),所以为什么ClassA不能编译。要解决它,我不能添加一个throws语句到 ClassA.example(),因为它会违反 ClassAInterface 。如果我只是在try catch语句中包含 ClassB.exampleTest(),其余的大部分 ClassA.example()将抛出错误,因为 ClassB 表示某事是错误的,您应该在该点抛出异常,这是抛出异常的整个点。所以我不知道为什么会发生这种情况或如何解决它。

So the problem that I'm having is that I'm getting an Unhandled Exception type ExampleException() error in the ClassA.example() method. Why does this happen? I want the ClassB method to throw the exception in specific cases, and signify that it sometimes throws exceptions with throws ExampleException(), so why does ClassA not compile. To solve it, I can't add a throws statement onto ClassA.example() because it would violate ClassAInterface. And if I just wrap ClassB.exampleTest() in a try catch statement, much of the rest of ClassA.example() would throw errors because ClassB signifies something is wrong and you should throw an exception at that point, which is the entire point of throwing an exception`. So I'm not sure why this is happening or how to solve it.

推荐答案

为什么代码拒绝编译?

Why does the code refuse to compile?

很正常如你所说, B.exampleTest()有时会抛出异常。 A.example()调用 B.exampleTest()并没有捕获任何异常。所以即使异常并不总是发生,有时会发生这种情况。而当它发生时,异常也由 A.example()抛出。所以它必须被宣布或被捕获。就像 B.exampleTest()有时会引发异常, A.example()有时会抛出同样的异常。

It's quite normal. As you said, B.exampleTest() sometimes throws an exception. A.example() calls B.exampleTest() and does not catch any exception. So, even if the exception doesn't always happen, it happens sometimes. And when it happens the exception is also thrown by A.example(). So it has to be declared or caught. Just like B.exampleTest() sometimes throws an exception, A.example() also sometimes throws the same exception.

现在,该怎么办?您有3个选择:

Now, what to do? You have 3 choices:


  1. 声明抛出异常:的调用者A.example() code>然后必须决定在这种情况下该怎么做。

  2. 内部获取A.example()并以某种方式处理它。

  3. 捕获它,将其包装在运行时异常(不必在throws子句中声明),并抛出此运行时异常。调用者将必须处理此运行时异常,但编译器不会有任何提示可以抛出异常。

  1. declare that the exception is thrown: the caller of A.example() will then have to decide what to do in that case.
  2. catch it inside A.example(), and deal with it somehow.
  3. catch it, wrap it inside a runtime exception (which doesn't have to be declared in the throws clause), and throw this runtime exception. The caller will have to deal with this runtime exception, but won't have any hint from the compiler that an exception can be thrown.

这篇关于使用java中的接口和类处理异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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