我想实现一个目标。我正在用演示代码分享我的概念。
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
。但是我希望通过父类型来捕捉它。这就是我定义制造商界面的原因。我们知道接口可以指向类对象。
但我看到一个错误
incompatible types: NotFound cannot be converted to Throwable
catch(NotFound e){
^
1 error
你能告诉我如何解决它吗?
答案 0 :(得分:4)
您只能throw
和catch
个Throwable
子类型的对象(通常是Exception
或RuntimeException
的子类型。
由于这些是类,我们在Java中没有多重继承,这意味着您必须在一个层次结构下适合您的异常。
如果您的NotFound
是特定的ServiceException
,则可以对其进行建模(同样,您可能需要添加Exception
后缀以遵循惯例):
class NotFoundException extends ServiceException
然后,让你的其他“未找到”例外延伸到这一点,例如:
class TeacherNotFoundException extends NotFoundException
(请注意,TeacherNotFoundException
也是ServiceException
)
然后,您将能够捕获所有“未找到”的例外情况:
catch(NotFoundException e){
}
答案 1 :(得分:0)
来自Java Doc link
catch块是一个异常处理程序,它处理由其参数指示的异常类型。参数类型ExceptionType声明了处理程序可以处理的异常类型,并且必须是从Throwable类继承的类的名称。处理程序可以使用名称引用异常。
所以如果你想在catch块中捕获 NotFound ,那么它应该继承自 Throwable 。