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

3

6

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?

Sainted answered 5/7, 2010 at 12:40 Comment(4)
If you posted some concrete question(s) (and your own reasoning about the possible answers), we could help better.Emalia
I'm not sure if this "human compiler" skill is all that useful, honestly, beyond some basic working understanding of the language and the API. It's pretty much guaranteed that there will always be a Java snippet that even James Gosling can't tell if it will compile and/or what it's actually doing.Overfly
The problem is that for the SCJP, you really are required to be able to state whether the error (if any) will be evoked at runtime or at compileSainted
@Peter Torok I'm sorry my question wasn't that clear but it was a bit hard to explain. If you read my comment on Andreas_D's answer it might make things a little bit more clear I suppose.Sainted
S
11

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)

Siren answered 5/7, 2010 at 12:44 Comment(3)
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.Sainted
@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.Overfly
@Sainted - 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 ;-)Siren
O
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

Overfly answered 5/7, 2010 at 12:43 Comment(3)
See also: #2705040, #2927354, #2859299Overfly
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.Sainted
@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/…)Overfly
P
0

Basically Runtime errors are logical errors in your code, even if the code is syntactically correct. Compiler errors refer to errors in your syntax/semantics. If you have a compiler error in your code, the program will never get to run (and check the logic of the code). If you have both syntactic and logical errors, you will first get the compiler error (syntax error) and then when you run the code again you will get a runtime error (logical error).

Privilege answered 20/11, 2017 at 15:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.