[英]Why am I getting unreported Exception?
Why am I getting the error message "unreported exception StupidNameException; must be caught or declared to be thrown"?为什么我收到错误消息“未报告的异常 StupidNameException;必须被捕获或声明为抛出”?
Here are my code blocks:这是我的代码块:
/**
* @throws StupidNameException
*/
abstract class Person {
private String firstName;
private String lastName;
public Person(String firstName, String lastName) throws StupidNameException {
if (firstName == "Jason") {
throw new StupidNameException("first name cannot be null");
}
this.firstName = firstName;
if (lastName == "Harrisson") {
throw new StupidNameException("last name cannot be null");
}
this.lastName = lastName;
}
// Rest of class goes here ...
}
class Student extends Person {
private String ultimateGoal;
private double GPA;
/**
* @throws StupidNameException when name is "Jason" or "Harrisson"
*/
public Student(String firstName, String lastName, String ultimateGoal, double GPA) {
super(firstName, lastName);
this.ultimateGoal = ultimateGoal;
this.GPA = GPA;
}
// Rest of class goes here ...
}
Look at the documentation you wrote yourself:查看您自己编写的文档:
/**
* @throws StupidNameException when name is "Jason" or "Harrisson"
*/
public Student(String firstName, String lastName, String ultimateGoal, double GPA) {
// ...
Where's the throws StupidNameException
? throws StupidNameException
在哪里? The Java compiler is wondering about that. Java 编译器对此感到疑惑。
Fix it accordingly:相应地修复它:
/**
* @throws StupidNameException when name is "Jason" or "Harrisson"
*/
public Student(String firstName, String lastName, String ultimateGoal, double GPA) throws StupidNameException {
// ...
This is necessary, because you're calling a super(firstName,lastName)
which by itself throws that exception.这是必要的,因为您正在调用一个
super(firstName,lastName)
,它本身会引发该异常。 It has either to be caught in a try-catch
, or, better, to be passed through by throws
.它必须要么被
try-catch
捕获,要么更好地通过throws
传递。
Because you are using "==" to compare Strings.因为您使用“==”来比较字符串。 You need to use equals() or equalsIgnoreCase() (if case does not matter) to compare string objects.
您需要使用equals()或equalsIgnoreCase() (如果大小写无关紧要)来比较字符串对象。 Change the following lines of code to what I have:-
将以下代码行更改为我所拥有的:-
if(firstName!=null && firstName.equals("Jason")) {
throw new StupidNameException("first name cannot be null");
}
this.firstName = firstName;
if(lastName!=null && lastName.equals("Harrisson")) {
throw new StupidNameException("last name cannot be null");
}
Although I am not sure why you want throw Null exception when the name is "Jason" or "Harrison".虽然我不确定当名称为“Jason”或“Harrison”时为什么要抛出 Null 异常。 They are clearly not null.
它们显然不是 null。
It is a better practice to throw IllegalArgumentException for arguments that you expect to be not null instead of your custom exception like you are doing now.更好的做法是为您希望不是 null 的 arguments 抛出IllegalArgumentException而不是像您现在所做的自定义异常。
Since you updated your post with a proper question, as @BalusC mentioned your problem lies in not defining your exception class.由于您用正确的问题更新了您的帖子,正如@BalusC 提到的,您的问题在于没有定义您的异常 class。 However, you still need to fix what I mentioned in my answer as well.
但是,您仍然需要修复我在回答中提到的内容。
Anyway.反正。 Some method you call in your code throws StupidNameException.
您在代码中调用的某些方法会引发 StupidNameException。 You must handle it in
try {... } catch ()...
or add throws StupidNameException
to the method where you call it.您必须在
try {... } catch ()...
中处理它,或者将throws StupidNameException
添加到您调用它的方法中。
In your case, the "method" is the constructor of Student
.在您的情况下,“方法”是
Student
的构造函数。 Because the constructor of super class throws Stupid...
, subclass'es constructor must also throw, or you must do try { super(... ) } catch(... ) {... }
.因为 super class 的构造函数会抛出
Stupid...
,所以子类的构造函数也必须抛出,否则必须try { super(... ) } catch(... ) {... }
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.