Java - When is it a compiler error and when is it a runtime exception?

28,404

Solution 1

Compile time error - the java compiler can't compile the code, often because of syntax errors. Typical candidates:

  • missing brackets
  • missing semicolons
  • access to private fields in other classes
  • missing classes on the classpath (at compile time)

Runtime error - the code did compile, can be executed but crashes at some point, like you have a division by zero.

  • using variable that are actually null (may cause NullPointerException)
  • using illegal indexes on arrays
  • accessing resources that are currently unavailable (missing files, ...)
  • missing classes on the classpath (at runtime)

('Crashes' is really not the correct term and is only used to illustrate what happens)

Solution 2

There is no easy answer to this; to see if something will compile, you have to completely understand the language specification and the API involved. You essentially have to act like a compiler, and no one can do this perfectly. Even compilers don't always follow the specification perfectly.

There are many, MANY corner cases in the Java language. This is why things like Java Puzzlers are so intriguing: people can't always tell if something would even compile and/or if it does, what's really going on.

Some of the more complicated areas of the Java language are:

  • Generics (Eclipse and javac compiler can't even agree on everything)
  • Method overloading resolution (one of the hardest to understand section of JLS)

Related questions

Share:
28,404

Related videos on Youtube

Michael
Author by

Michael

Updated on May 20, 2020

Comments

  • Michael
    Michael almost 4 years

    I am currently studying for the SCJP certification using the Sierra and Bates Study Guide and in many of the self tests (mock exam questions) I keep running into the same problem - I can't tell whether a particular error will be at runtime (an exception) or at compile (compile error). I know this is a bit of a vague question and that it might not be possible to answer but, how can I tell if an error will be found at compile or at runtime? Would you be able to send me some website links that might be able to help me?

    • Péter Török
      Péter Török almost 14 years
      If you posted some concrete question(s) (and your own reasoning about the possible answers), we could help better.
  • polygenelubricants
    polygenelubricants almost 14 years
  • Michael
    Michael almost 14 years
    Thanks, this answer is very useful. However, sometimes it seems that compiler "knows" that the code is going to crash at runtime and it's a compile error. Other times, it's like one "guarantees" to the compiler that what you are coding will work - and if it doesn't, the code crashes at runtime. (downcasting of reference variables comes to mind) This is what makes telling if it will be compile or runtime tricky for me.
  • polygenelubricants
    polygenelubricants almost 14 years
    @Michael: I think the answer is that you just have to learn the language and the API at least to the point where it's practical. You will never be a perfect human compiler, which isn't all that useful skill to have anyway.
  • Andreas Dolk
    Andreas Dolk almost 14 years
    @Michael - one prominent error is the unreachable code compile time error. If you see some code, that qualifies for this error, you wouldn't even guess, that it can't be compiled ;) There are some tricky errors that you just have to know - for SCJP only. In real life you can rely on your IDE and your friendly JVM ;-)
  • Michael
    Michael almost 14 years
    Whilst I didn't find the answer very clear and thus didn't understand it fully, I thank you for the time you took and for your interest.
  • polygenelubricants
    polygenelubricants almost 14 years
    @Michael: if you have specific confusion with examples, ask another question and I'd be more than happy to elaborate verbosely to the best I can. It looks like maybe you have some question about when instanceof is a compile-time error vs and when it throws ClassCastException, perhaps? Search around and perhaps that's already asked/answered, but in any case JLS 15.20.2 is quite clear on how it should behave (java.sun.com/docs/books/jls/third_edition/html/…)

Related