views:

92

answers:

2

Hello all, 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?

Thanks in advance, Mike

+2  A: 

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

polygenelubricants
See also: http://stackoverflow.com/questions/2704540/auto-unboxing-fail-for-compound-assignment, http://stackoverflow.com/questions/2926854/eclipse-bug-switching-on-a-null-with-only-default-case, http://stackoverflow.com/questions/2858799/generics-compiles-and-runs-in-eclipse-but-doesnt-compile-in-javac
polygenelubricants
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.
Michael
@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 (http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.20.2)
polygenelubricants
+1  A: 

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 ressources 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)

Andreas_D
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.
Michael
@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.
polygenelubricants
@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 ;-)
Andreas_D