• Post Reply Bookmark Topic Watch Topic
  • New Topic

Dealing with exceptions  RSS feed

 
Ranch Hand
Posts: 234
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys, I have a few questions about exception. Take this code here (originally from  Glenn, Mitchell. OCAJP Oracle Certified Associate Java SE 8 Programmer Practice Exams (Kindle Location 9313). Enthuware. Kindle Edition. )

I got a little confused and thought that it didn't compile because it wasn't handling the Exception correctly, but it does, so I was hoping to run the whole thing past you.
With checked exception we have the catch or declare requirement, so that you either declare that a method throws and exception or you have a try catch statement. In this case the method luckyNumber throws an exception but it has a try catch block so it doesn't need to declare that it throws an exception. So when the exception is thrown inside the method the catch will deal with it and everybody's happy.
Now, if I add throws exception to the method like so the code doesn't compile anymore and I'm not entirely sure why: I mean the method still throws an exception and it still catches it inside its own catch statement, but with the addition of the throws now I have to change the main method to even if the exception will never reach the main method as it is caught inside the luckyNumber method...correct?
 
Saloon Keeper
Posts: 3330
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Your throw is already surrounded by a try/catch block and will not be visible outside of the block. So, the method itself will never throw an exception.
 
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would say that throwing an Exception and catching it in the same method is simply an inefficient surrogate for if‑else and shou‍ld be avoided. Also a return in catch and a return in finally constitute confusing code. Which will the method return? That method shou‍ld be writtenThat will be interpreted as
seed > 10 ? seed % 10 : (seed %  2 == 0 ? 5 : 0);
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The reason for the underlining and () is that ?: associates to the right.
The reason for the 5 is that I am keeping quiet about whether the method will return 3 or 7. I shall let you work that out for yourself.
 
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

To give more detail on the original question... The reason you can declare the "throws exception" on the method, is because the Exception class is a superclass to the RuntimeException class, which in turn, is an unchecked exception. This means, even though the compiler knows that the luckyNumber() method will never throw the Exception exception, it doesn't know whether or not, it won't throw an unchecked subclass of it, and hence, it must allow the declaration.

Additionally, this information is not available when compiling the main() method. The compiler knows that the luckyNumber() method throws an Exception, which includes both checked and unchecked exceptions. Hence, the main() method must either handle, or declares as throws, of the checked exceptions that IS-A Exception.

Arguably, it is a bit weird. The throws is allowed due to possible unchecked exceptions, but the main() must be enforced, due to possible checked exceptions (which while obviously to us, but not to the compiler, that it is not possible).

Henry

 
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jason Attin wrote:. . . Now, if I add throws exception to the method like so the code doesn't compile anymore . . .
To add to what Henry says (and actually to read the original question):-
If you put plain simple throws Exception as part of the method header, you are telling the compiler that the method or an overridden version of it might throw an Exception. You are allowed to override that method to throw any kind of Exception, and the calling method (main()) will have to deal with it somehow. Henry says we can see that the Exception is never thrown, but you are telling the compiler to ignore that fact,  by giving it a warning, “expect any kind of Exception from this method, even if it is overridden.”
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!