• Post Reply Bookmark Topic Watch Topic
  • New Topic

When does Checked exception occur, at run time or compile time ?  RSS feed

 
Ganish Patil
Ranch Hand
Posts: 529
19
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know about unchecked exception that occurs at run time but don't know about checked. I found a video says checked exception and unchecked exception occurs at run time only. Also found a website which says checked exception occurs at compile time. I use Java 8 Documentation but didn't find when checked exception occurs at compile time or at run time. If you read in Java Documentation please give me reference if possible.
 
Stephan van Hulst
Saloon Keeper
Posts: 7974
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All exceptions (except those thrown by the compiler when you try to compile bad code) are always thrown at runtime. The difference between checked and unchecked (sub-classes of RuntimeException) exceptions is whether the compiler forces you to write code that handles the exception.



The compiler will throw an exception during compilation, but it's not the same exception as the one thrown in bar(). The exception thrown in bar() (after you declare that the method throws a checked exception) will still only be thrown during runtime of your application.
 
Stephan van Hulst
Saloon Keeper
Posts: 7974
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
RuntimeException really is a misnomer. They should have called it CheckedException instead.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:RuntimeException really is a misnomer. They should have called it CheckedException instead.
UncheckedException, you mean?

The theory is that the runtime Exception occurs entirely within the Java® runtime rather than at the interface between Java® and other code. An IOException occurs when you attempt to access the hard drive or network or keyboard, for example. That is why they called in runtime Exception. At least that it the theory; most people can find exceptions to that notion.
 
Paweł Baczyński
Bartender
Posts: 2083
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One important thing.

Exceptions do not occur at compile time. Period.

A compilation error is not an Exception.
 
Ganish Patil
Ranch Hand
Posts: 529
19
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Pawel
Exceptions do not occur at compile time. Period. A compilation error is not an Exception.
thats what I was trying to confirm. Thank you.
 
Stephan van Hulst
Saloon Keeper
Posts: 7974
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:UncheckedException, you mean?


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