• Post Reply Bookmark Topic Watch Topic
  • New Topic

oca preparation exception  RSS feed

 
fahmida tasnim prema
Greenhorn
Posts: 19
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
code is from kathy sierra oca study guide book.


this code gives compiler error because someMethod() did not handle the code or declare it using throws keyword .but my question is that even if we handle it in someMethod() by using try catch block that try catch will never run instead the try catch inside doStuff() method will run . so whats the point of handling it in someMethod() ? is just to avoid compilation error?
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fahmida tasnim prema wrote:
this code gives compiler error because someMethod() did not handle the code or declare it using throws keyword .but my question is that even if we handle it in someMethod() by using try catch block that try catch will never run instead the try catch inside doStuff() method will run . so whats the point of handling it in someMethod() ? is just to avoid compilation error?


That is the whole point of a checked exception. The compiler is checking to confirm that you know about the exception.

So, you have two choices. One, you can actually handle the exception in the method. Or two, you can *not* handle the method, and tell the compiler so, by declaring the method can throws the exception.  You can't do neither, and treat it as an unchecked exception.

And BTW, your dostuff() method does throw an MyException exception, so, I don't know what you mean by "even if we handle it in someMethod() by using try catch block that try catch will never run". Based on the code, it will definitely trigger the catch for the checked exception.

Henry
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:Based on the code, it will definitely trigger the catch for the checked exception.


To discuss this one a bit more...

If you remove the "throws" declaration from the dostuff() method, and that is the only change that you make, you will see that the compiler will complain. The reason is the method actually throws an MyException exception at line 10.

It is actually pretty easy to trick the compiler to think an exception will be thrown, when it won't at run time (based on the use case) ... but in this case, the compiler did get it right...

Henry
 
João Victor Gomes
Ranch Hand
Posts: 110
11
Eclipse IDE Java Netbeans IDE Postgres Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry already explained it well. But I'll just want to exemplify:

Now let's see this next code

So, basically, the catch of doStuff() catches the exception, but then throws it again to the caller, so the doStuff() method is required to declare it in its signature, since it is a checked exception
Even if we remove the lines where the exceptions are actually thrown, we still need to handle inside someMethod(), like this

That is true, because we can declare an exception, but the method are not required to actually throw that exception in its body. But the caller of the method will need to handle or declare that exception.
So, if a method declares a checked exception, another method that calls that method is required to handle or declare the exception.
 
João Victor Gomes
Ranch Hand
Posts: 110
11
Eclipse IDE Java Netbeans IDE Postgres Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And of course, when I say "But I'll just want to exemplify: ", I mean "But I'll just exemplify: " or "But I just want to exemplify: " haha
 
fahmida tasnim prema
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
And BTW, your dostuff() method does throw an MyException exception, so, I don't know what you mean by "even if we handle it in someMethod() by using try catch block that try catch will never run". Based on the code, it will definitely trigger the catch for the checked exception.


i meant this .In this code the output will be inside do stuff. so if we even handle it in someMethod() it is actually no use.  sorry for not explaining things properly .


[HENRY] Fixed formatting
 
fahmida tasnim prema
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how can i edit my reply ? i did not use tag properly
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fahmida tasnim prema wrote:how can i edit my reply ? i did not use tag properly


Give me a second. I can fix it for you.... DONE

Henry
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fahmida tasnim prema wrote:
i meant this .In this code the output will be inside do stuff. so if we even handle it in someMethod() it is actually no use.  sorry for not explaining things properly .


The issue here has to do with the doStuff() method. You declared that method as throws MyException. Hence, the someMethod() method needs to either handle the exception or also declares as throws.

In other words, you know that the exception is not thrown from the doStuff() method. You told the compiler that the exception is thrown from the doStuff() method. So, why are you confused that the compiler believes that the exception is thrown? ...


... so ... I guess the follow up question to this would be, why does the compiler allows you to declare that the exception is thrown, when clearly it is not?  Well, it has to do with the fact that the throws is part of the method interface, and the body of the method has to do with the implementation.

The throws declaration could be part of an interface that is implemented, or the declaration could be inherited from the super class interface. And regardless, even in this example, you may decide, in the future, to override the method with an implementation that throws that exception in the subclass. And since the throws declaration of the doStuff() method is part of the definition of the interface, and not of a particular implementation, the compiler can't really check it against a particular implementation.

Henry
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!