• 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Difference between compile time exceptions and runtime exceptions

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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



 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Marshal
Posts: 73994
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot! I got clarity with this part now.
 
Campbell Ritchie
Marshal
Posts: 73994
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's a pleasure:)
 
Anderson gave himself the promotion. So I gave myself this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic