我想实现一个目标。我正在通过演示代码分享我的概念。
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/