• Post Reply Bookmark Topic Watch Topic
  • New Topic

Difference between compile time exceptions and runtime exceptions  RSS feed

 
Saishradha Mohanty
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please help me out with the difference between compile time exceptions and run time exceptions, how do these 2 work at the compiler level?
And can runtime exceptions be caught too?If so, then how, because runtime exceptions are unchecked exceptions, then how can they be caught?
 
Stephen Goldberg
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I do not understand what you mean at the compiler level.

However, a compile-time exception happens when you attempt to compile the code. Usually compile time errors are identified, if using and ide, with red squiggly lines - such as misspelled words in a text editor.

eg:



A run-time error is something that happens at run-time, whereas the code was able to be compiled without error. These are often not identified by red squiggly lines.

eg:


Run time exceptions can be caught.

eg:



The above code catches the index out of bounds exception
displays a default message
displays our custom message
then continues with the program.

You can learn more about exceptions and handling exceptions here:
Exceptions



 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is no such thing as "compile time exceptions". Exceptions are always thrown and caught at runtime - never at compile time.

Maybe you got confused because there is a class RuntimeException, which made you think "if there is a runtime exception, then there must also be a compile time exception". But that is not how it works.

There are three kinds of exceptions: checked exceptions, unchecked exceptions (also called "runtime exceptions") and errors. Oracle's tutorial about exceptions explains this in more detail.

Stephen Goldberg wrote:However, a compile-time exception happens when you attempt to compile the code. Usually compile time errors are identified, if using and ide, with red squiggly lines - such as misspelled words in a text editor.

That is not called an "exception" - that's just a compilation error. Calling this a "compile time exception" is confusing, because it doesn't have anything to do with throwing and catching exceptions in your code.
 
Stephen Goldberg
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong wrote:
That is not called an "exception" - that's just a compilation error. Calling this a "compile time exception" is confusing, because it doesn't have anything to do with throwing and catching exceptions in your code.


Oops, mixed his words with mine. Notice the I called it correctly mid sentence lol
Thanks for the correction.
 
Saishradha Mohanty
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But if all the exceptions are thrown at runtime only, then why is it that the compiler shows error during compilation for checked exceptions and some error messages during runtime for runtime exceptions?
 
Saishradha Mohanty
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay okay, got it!I got where I was going wrong.
So does it go like, in case of checked exceptions the compiler checks whether any code has been given for handling the exceptions that might be thrown that is why if try catch blocks are not written then there is an error message by the compiler. But in case of runtime exceptions, the compiler doesn't check whether there is some code to handle if any type of run time exception is thrown, so, it compiles without any error even if no try/catch blocks are written. But if any exception is thrown at the run time then the error is displayed on the screen and the process terminates.

Please verify if what I understood is correct or not.
Thanks a lot!
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
SG: Welcome to the Ranch

Don't say runtime exception. Say unchecked exception, because this isn't a RuntimeException but it is an unchecked exception. All exceptions occur at runtime; the name RuntimeException was chosen because it represents exceptions whose origin is entirely inside the runtime as opposed to something like an IOException which might originate outside the runtime, e.g. in your hard drive. It was thought that it will not be possible to cure RuntimeExceptions by simply running the code again, which is why they are all unchecked.
Apart from that, you are correct The compiler checks whether checked exceptions have been handled and will fail to compile the code if any have been forgotten about. You will also have problems with this sort of code:-I shall leave you to find out for yourself what happens.
 
Saishradha Mohanty
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot! I got clarity with this part now.
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's a pleasure
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!