- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想实现一个目标。我正在通过演示代码分享我的概念。
interface NotFound{
//define one marker interface which will mark not found
}
class ServiceException extends RuntimeException{
}
class TeacherServiceException extends ServiceException{
}
class TeacherNotFoundException extends TeacherServiceException
implements NotFound{
TeacherNotFoundException(){
//if teacher is null then will throw this type of exception
}
}
class StudentServiceException extends ServiceException{
}
class StudentNotFoundException extends StudentServiceException
implements NotFound{
StudentNotFoundException(){
//if student is null then will throw this type of exception
}
}
interface HasName{
void addName(String name);
}
//these are the models
class Student implements HasName{
@Override
public void addName(String name){
}
}
class Teacher implements HasName{
@Override
public void addName(String name){
}
}
//these are the resources
class StudentResource{
StudentService service = new StudentService(null);
NameSupportResource<TeacherService> support = new NameSupportResource<>();
void addName(String name){
support.addName(service,name);
}
}
class TeacherResource{
TeacherService service = new TeacherService(null);
NameSupportResource<TeacherService> support = new NameSupportResource<>();
void addName(String name){
support.addName(service,name);
}
}
class NameSupportResource<T extends HasNameService>{
void addName(T service,String name){
try{
service.addName(name);
}
catch(NotFound e){
}
}
}
//these are the services
interface HasNameService<T extends HasName>{
T addName(String name);
}
class StudentService implements HasNameService<Student>{
Student student;
StudentService(Student student){
this.student = student;
}
@Override
public Student addName(String name){
if(student == null)
throw new StudentNotFoundException();
else{
student.addName(name);
return student;
}
}
}
class TeacherService implements HasNameService<Teacher>{
Teacher teacher;
TeacherService(Teacher teacher){
this.teacher = teacher;
}
@Override
public Teacher addName(String name){
if(teacher == null)
throw new TeacherNotFoundException();
else{
teacher.addName(name);
return teacher;
}
}
}
在这里您可以看到,如果 studnet 为 null 或 teacher 为 null,则它可以分别抛出 StudentNotFoundException
或 TeacherNotFoundException
。
使用NameSupportResource
的目的是为了通用。因为将来我可以引入另一种模型,如 Student
或 Teacher
它们可以具有相同的名称添加操作。现在,如果任何模型为 null,我想抛出子类型异常,即 StudentNotFoundException
。假设我想介绍另一个模型,即书,如果书为空,那么我将抛出 BookNotFoundException
。但我想通过父类型来捕捉它。这就是我定义制造商界面的原因。正如我们所知,接口(interface)可以指向类对象。
但是我看到了一个错误
incompatible types: NotFound cannot be converted to Throwable
catch(NotFound e){
^
1 error
能告诉我怎么解决吗??
最佳答案
您只能throw
和catch
对象是Throwable
的子类型(通常是Exception
或RuntimeException
).
由于这些是类,我们在 Java 中没有多重继承,这意味着您必须将异常放在一个层次结构下。
如果您的 NotFound
是一个特定的 ServiceException
,您可以像那样对其进行建模(另外,您可能希望将 Exception
后缀添加到遵循惯例):
class NotFoundException extends ServiceException
然后,让您的其他“未找到”异常扩展此异常,例如:
class TeacherNotFoundException extends NotFoundException
(注意 TeacherNotFoundException
也是一个 ServiceException
)
然后您将能够捕获所有“未找到”异常:
catch(NotFoundException e){
}
关于java - NotFound 无法转换为 Throwable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44846945/
我正在尝试弄清楚如何将 Throwable\/List[Throwable\/A] 干净利落地排列成 Throwable\/List[A] ,可能使用 List 的 Traverse 实例,但我似乎无
在不创建新 Throwable 的情况下添加一种通用方法来向 Throwable 添加信息是否有益? 我经常看到这样的代码: try { foo(); } catch (Exception e
当发生异常并且我得到 Throwable 时,我想将它转换为 MyThrowable 并重新抛出它。 我正在实现实例创建的方法 obs1 = obs1.map(new Func1() {
我有一个带 @Aspect 注释的类正在调用 ProceedingJoinPoint#proceed() 。此方法抛出 Throwable,因此该类看起来像这样: @Aspect @Component
我知道网络上充斥着不要捕获 Throwable 的建议,但是它适用于使用 CompleteableFuture 吗?例如,在 ExecutorService es = Executors.ne
这个问题在这里已经有了答案: No Exception while type casting with a null in java (10 个答案) 关闭 7 年前。 这个问题只是让我理解这个概念
假设我有几种方法可以返回 \/[Throwable, String]。右边的值 Int 是我想要的,左边的值累积错误。 import scalaz._ import Scalaz._ type Err
今天我去部署一个我创建的 Java 应用程序到 Google App Engine,但我遇到了一些非常无用的错误消息。 Invocation of init method failed; nested
我正在实现一个 AOP 拦截器,它处理 RetryTemplate 中的逻辑。问题是 ProceedingJoinPoint#execute 被声明为抛出 Throwable,但 RetryTempl
首先我必须说我阅读了以下内容:http://docs.oracle.com/javase/tutorial/essential/exceptions/index.html ! 尝试编译时出现的错误如下
我正在编辑别人的代码,并且一个方法有“Throws Throwable”。我把它去掉了,所以 Eclipse 会让我只添加它需要抛出的异常类型...但是,我在调用父类(super class)(我当前
当我开发 android 应用程序时,我想制作一个 CrashReport 类,然后使用它向我的服务器发送报告。 我做了一个名为CrashHandler的类,它实现了UncaughtException
我在java中得到了这段代码: public static void main(String[] args) { try{ Class tryLoadi
我正在使用 JDK8 在 macOS 上工作。 在 catch 中,我必须给出异常的完整名称,例如在这种情况下 (ArithmeticException e) 而不是 (Exception e)运行代
我有一个执行 I/O 的类(我们称之为 ABC)。像 FileOutputStream.close 这样的东西会让你在它们周围使用 try catch block 。此外,我创建了自己的可抛出对象,帮
这个问题在这里已经有了答案: 关闭 10 年前。 Possible Duplicate: Why doesn’t Java allow generic subclasses of Throwable
我在这里看到了很多关于 Exception 和 Throwable 之间区别的一般性问题。我知道其中的区别,而且我有一个更具体的问题。 我正在编写一个库,用于绑定(bind)并运行多个用户提供的代码片
我已经捕获了任何未捕获的异常 Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
我有一些外部提供的回调要运行。因为它们可以包含任何东西,所以我更愿意冒险在它们上捕获 Throwable 并因此从任何可恢复的错误中恢复。 回调执行的某些阶段允许抛出错误,除非错误连续重复两次。在这种
在java语言中,错误类的基类是java.lang.Error,异常类的基类是java.lang.Exception。 1)相同点:java.lang.Error和java.lang.Excepti
我是一名优秀的程序员,十分优秀!