Reputation: 23
I have a debug assignment for class that I cannot seem to figure out. We are told to debug the assignment so that the output for the assignment looks as such.
Output:
There is a problem with the Eagle!
Java Result: 9999
The code given is as follows:
//Superclass for the custom exception.
public class EagleLandingException extends Exception
{
public EagleLandingException(String msg)
{
super(msg);
}
}
and the main class
public class ThrowEagleExceptionTest {
public static void main(String[] args) {
try {
EagleLanding();
}
catch (EagleLandingException badEagle) {
System.out.printf("%s\n", badEagle.getMessage());
System.exit(9999);
}
}
private static void EagleLanding () {
EagleLandingException("There is a problem with the Eagle!");
System.exit(9999);
}
I get an error that "the exception is never thrown" at the line
catch (EagleLandingException badEagle)
and also receive a "cannot find symbol" error on line
EagleLandingException("There is a problem with the Eagle!");
I don't understand why this is happening and have looked at other questions already posted but can't seem to figure out what the issue is. Thank you in advance.
Upvotes: 1
Views: 6211
Reputation: 22422
The problem is with this line EagleLandingException("There is a problem with the Eagle!");
which is syntactically incorrect, therefore needs to be changed as below:
private static void EagleLanding () throws EagleLandingException{
//the below statement creates a new EagleLandingException and then hrowing it
new throw EagleLandingException("There is a problem with the Eagle!");
}
The main rule with Checked Exception is that either the method has to handle the exception (using catch
block) or it has to be declared in the method signature using throws
keyword.
Also, any statements after throw
statement will NOT be executed (except finally
block (if any) for the try
), therefore I removed System.exit
The important point that you need to take is that you are actually creating/throwing a checked exception which in general, NOT PREFERRED and is NOT required (unless you have some recovery policy upon throwing/receiving the exception), so my better suggestion is to convert your code using RuntimeException
as below:
//Extend with RuntimeException instead of Exception
public class EagleLandingException extends RuntimeException
{
public EagleLandingException(String msg)
{
super(msg);
}
}
public class ThrowEagleExceptionTest {
public static void main(String[] args) {
try {
EagleLanding();
}
catch (EagleLandingException badEagle) {
System.out.printf("%s\n", badEagle.getMessage());
System.exit(9999);
}
}
//now you don't need to declare the EagleLandingException
private static void EagleLanding () {
throw new EagleLandingException("There is a problem with the Eagle!");
}
Upvotes: 0
Reputation: 718788
The problem is here.
private static void EagleLanding () {
EagleLandingException("There is a problem with the Eagle!");
System.exit(9999);
}
EagleLandingException(...)
is being interpreted as a call to a method called EagleLandingException
. But that is not a method. It is a constructor, and its name (a class name) is not in the namespace where Java looks for method names. The compilation error is (in effect) saying "I cannot find a method called ...".
The other problem is that your EagleLanding
method is not declared as throwing the exception. So, when you then try to catch the exception in the caller, the compiler says "this is a checked exception, and since the exception is not declared as thrown, it cannot occur here". Hence the compilation error.
The correct way to write this is:
private static void eagleLanding () throws EagleLandingException {
throw new EagleLandingException("There is a problem with the Eagle!");
}
Notes:
new
causes the exception object to be createdthrow
causes exception to be thrown.throws
clause declares that the method throws the checked exception.throw
are unreachable. If you don't remove them, you will get a compilation error. (And, besides calling exit
in a method like that it a bad idea ...)In his answer, @javaguy suggests that EagleLandingException
should be unchecked; i.e. declared as a subtype of RuntimeException
.
Is this right? Maybe yes, maybe no. In the (highly artificial) context that you have given us, it is impossible to know for sure.
However, there are some general guidelines that the Java designers recommend developers should follow:
Checked exceptions are intended for anticipated events where something may be done about the exception.
Unchecked exceptions are intended for unanticipated events (e.g. bugs) where (typically) there is not much that a programmer could do.
The other criterion people use in deciding whether to use checked or unchecked exceptions is to minimize the amount of boilerplate code; i.e. the throws
clause. A lot of people find them annoying, and will go to considerable lengths to avoid them; e.g. by declaring all custom exceptions as unchecked, wrapping standard exceptions in custom exceptions, and so on.
I think that the best path is somewhere in between. Carefully consider the implications of declaring each exception as checked or unchecked, and use a hierarchy of exceptions to manage the size of throws
declarations.
Upvotes: 2
Reputation: 395
change
EagleLandingException("There is a problem with the Eagle!");
to
throw new EagleLandingException("There is a problem with the Eagle!");
Also, since EagleLandingException is a checked one, you'll need to add it to EagleLanding () method signature.
private static void EagleLanding () throws EagleLandingException {
explanation:
you need to create instance of your exception before you can throw it. In java this is done through new operator.
The last line in EagleLanding is never executed because an exception is thrown before it and this causes method to exit prematurely. The control then goes into your catch block.
Upvotes: 0
Reputation: 22089
The problem is that you never throw the exception, you do not even create it. The line EagleLandingException("There is a problem with the Eagle!");
does nothing, but cause an error. You need to employ the throw
keyword like this:
private static void EagleLanding () throws EagleLandingException {
throw new EagleLandingException("There is a problem with the Eagle!");
}
You can also omit System.exit(9999);
, as it is never reached, because the exception interrupts your program flow an goes straight back into the catch
branch. The throws
declaration in the method declaration signals, that you do not handle the exception in the method EagleLanding
.
Upvotes: 1