[英]NotFound cannot be converted to Throwable
我要實現一個目標。 我正在與演示代碼分享我的概念。
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
。 假設我要介紹另一個模型,即book,如果book為null,那么我將拋出BookNotFoundException
。 但我想按父類型來捕獲它。 這就是為什么我定義了制造商界面。 眾所周知,接口可以指向類對象。
但我看到一個錯誤
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 Doc 鏈接
catch塊是一個異常處理程序,用於處理其參數指示的異常類型。 參數類型ExceptionType聲明處理程序可以處理的異常類型,並且必須是從Throwable類繼承的類的名稱。 處理程序可以使用名稱引用異常。
因此,如果要在catch塊中捕獲NotFound ,則它應繼承自Throwable 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.