• Post Reply Bookmark Topic Watch Topic
  • New Topic

Exception Handling  RSS feed

 
jose chiramal
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator




This gives me an compile time error : Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Unreachable catch block for IllegalAccessException. This exception is never thrown from the try statement body

at ThrowsDemo1.main(ThrowsDemo1.java:20)

If i replace the "IllegalAccessException" with "Exception" in the catch block, there is no error. WHY SO ?
 
Dawn Charangat
Ranch Hand
Posts: 249
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
IllegalAccessException is a specific type of Exception, which get thrown in specific conditions only. The catch block could get to that only if your block of code could somehow create an illegal access, or could deliberately throw one, using the "throws" clause. Since your piece of code didnt do either, the code inside the catch block was unreachable.

The general "Exception" however would catch anything which confirms with a negative activity (except for an error) - ie, any runtime exceptions too. Hence the compiler would not be able to decide if the code inside the catch will be or will not be reached.

hence the result.
 
jose chiramal
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for you response.

You mentioned that "The catch block could get to that only if your block of code could somehow create an illegal access, or could deliberately throw one, using the "throws" clause. Since your piece of code didnt do either, the code inside the catch block was unreachable. "

The catch block accepts "Exception" and any runtime exceptions even though am not having an exception scenario within my code.
What I mean to say is since there is no exception occuring shouldn't it give "code not reachable error" even for "Excpetion" type and any "runtimeexception" as well. Why does it not ??? Why is it that only exception and runtime excpetion is treated in a different way..
 
Vijitha Kumara
Bartender
Posts: 4001
42
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jose chiramal wrote:.....What I mean to say is since there is no exception occuring shouldn't it give "code not reachable error" even for "Excpetion" type and any "runtimeexception" as well. Why does it not ???

IllegalAccessException is a checked exception as it extends directly from the Exception class so if the body of the try doesn't throw a one you can't catch it.
Why is it that only exception and runtime excpetion is treated in a different way..

Not sure what you meant by this, but RuntimeException itself extends from the Exception. Read more about Exceptions here
 
Jim Hoglund
Ranch Hand
Posts: 525
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jose : The compiler is just pointing out that you are trying to catch an
exception that can't be thrown. Try changing throwOne() like this. You
will also need to change your catch parameter to be type Exception.Jim ... ...
 
jose chiramal
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your replies. Here the compiler checks for "checked exceptions" at compile time and since we have a "checked exception" that we have mentioned in the catch block although there is no occurence of checked excpetions; the compiler complains at compile time.


Similary instead of the "checked exception" if i have a "unchecked exception(runtime exception)" shouldnt't the compiler complain at run time , since there is no occurence of "runtime exception" ?? The code am referring to same as in my first post.
 
Vijitha Kumara
Bartender
Posts: 4001
42
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jose chiramal wrote:Similary instead of the "checked exception" if i have a "unchecked exception(runtime exception)" shouldnt't the compiler complain at run time , ...

Compiler has nothing to do at the runtime. No it won't complain at compile time. That's why they are called *Unchecked* Exceptions.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!