Construtors in Java have limitations. Not everything is possible in them. Check fifty sixth question of full-stack dev quiz to check if you know what they are.
This is a code of one class in Java 11. It has a serious issue that prevents it from being usable. Actually, it will not even compile.
public class AppException extends Exception {
public AppException(int errorCode, Throwable cause) {
if (errorCode == 1) {
super(buildMessage(errorCode));
} else {
super(buildMessage(errorCode), cause);
}
}
private static String buildMessage(int errorCode) {
switch (errorCode) {
case 1:
return "User not found";
case 2:
return "Malformed request";
default:
return "Unknown error";
}
}
}
What is the compilation-time problem with that class? Choose the best answer.
Exception
class cannot be extended as it is final.AppException
constructor must have the same list of arguments as a construction fromException
class.- Calling the
super
constructor cannot be precedented by any other code. - A constructor cannot call a static method.
- A switch statement cannot return
String
.
For the correct answer scroll down
.
.
.
.
.
.
The correct answer is: c. If you would like to read more, check Logic in constructor or static factory method article.