• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Exception confusion

 
Ranch Hand
Posts: 257
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

causes an compiler error stating that IOException not thrown whereas

does not have an compiler error or runtime error.
Why is it so?
Thanks
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Checked Exceptions are very different from Unchecked Exceptions. As a programmer, you are considered "accountable" for handling checked exceptions appropriately. In the first case, you're trying to catch a checked exception that can never be thrown. This cuases an error.
Unchecked exceptions (as the name implies), go unchecked by the compiler. Exception and it's child RuntimeException are examples of unchecked exceptions. As the compiler doesn't know when or where an unchecked exception might be thrown (i.e. a NullPointException is an unchecked exception), it doesn't know that the code you have written is incapable of throwing such an exception.
As checked exceptions must be thrown by the programmer, the compiler knows that the exception won't be thrown, obviously, this isn't possible with unchecked exceptions.
Check out the JLS, §11.2 Compile-Time Checking of Exceptions, for more information.
I hope that helps,
Corey
[ March 20, 2003: Message edited by: Corey McGlone ]
 
sun par
Ranch Hand
Posts: 257
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Corey. Exception could hold its subclasses of CheckedException too, so I guess its making provisions considering it might hold RuntimeException.
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by sun par:
Thanks Corey. Exception could hold its subclasses of CheckedException too, so I guess its making provisions considering it might hold RuntimeException.


Right. Because catching a particular exception type will catch not only that exception but all child exceptions, it doesn't make sense to have Exception be a checked exception because you would then be requried to handle RuntimeExceptions as checked exceptions because RuntimeException is a child of Exception.
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The mother of unchecked exceptions is RuntimeException. So actually, Exception is a checked exception. This code won't compile:

The reason
catch (IOException e)
cause the compile-time error and
catch (Exception e)
doesn't is probably due to the fact that Exception encompasses both checked and unchecked exceptions. When you just catch Exception, there is really no way that the compiler can determine for sure if it needs to enforce the implied "if you catch a checked exception, there should be code that can throw it" rule. If the programmer merely wanted to catch a RuntimeException, the compile-time error would not be appropriate.
Because of this ambiguity, I always question code that catches Exception instead of more specific ones. Catching specific exceptions makes the code's intent much clearer and can help you avoid introducing hard-to-find bugs into code.
[ March 20, 2003: Message edited by: Junilu Lacar ]
 
Ranch Hand
Posts: 203
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I just want to add that any exception derived from Exception class is also treated by JVM as checked exception.
 
sun par
Ranch Hand
Posts: 257
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Causes a compiler error, whereas my first program did not cause any problems why
 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
it causes compiler error because the code in the try block will never throw BlueException which you made a checked exception.
but if you try to make BlueException extends RedException then the code will compile fine since as you define that the methd me may throw RedException then it could also throw BlueException as a subclass of RedException.
but one moment convert the order of the two catch clauses ( catch blueException first or it'll not compile)
 
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks guys. It cleared up a whole lot.
 
sun par
Ranch Hand
Posts: 257
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

This code compiles and runs without any problems where as BlueException,RedException causes compiler error. Why
 
mohamed hamdy
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
when you catch Exception then you didn't determined that you want to catch checked exception specifically since runtime exception could also be caught by this catch clause and the compiler dosn't check in this case wheather the code inside the try clause could throw a runtime exception or not.
there's a differnt case when you define that a method throws Exception then you could throw a checked exception (as a subclass of Exception )in the body of the method and this gives the compiler the responsibility to treat this method as it throws checked exception so it must be called inside try clause.
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sunita,
I think this JavaWorld article might help explain things: http://www.javaworld.com/javaworld/javatips/jw-javatip134.html
 
sun par
Ranch Hand
Posts: 257
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks. So when we subclass exception it is treated as an checked exception in this case.
 
mohamed hamdy
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by sun par:
Thanks. So when we subclass exception it is treated as an checked exception in this case.


yes you are right
 
sun par
Ranch Hand
Posts: 257
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Junilu, the article was good.
 
reply
    Bookmark Topic Watch Topic
  • New Topic