Java - When is this a compiler error and when is this an exception at runtime?

I am currently studying SCJP certification using the Sierra and Bates Study Guide and in many tests (exam questions). I am constantly facing the same problem. I can not say whether a particular error will be in runtime (exception) or during compilation (compilation error). I know this is a slightly vague question and it may not be possible to answer it, but how can I find out if a compilation error or runtime error will be detected? Can you send me some links to sites that could help me?

+3
source share
3 answers

Compile - time error - The Java compiler cannot compile code, often due to syntax errors. Typical candidates:

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

An error occurred while performing - the code is compiled, can be made, but at some time flies, as if you had a division by zero.

  • using a variable that is actually null(may throw a NullPointerException)
  • using invalid indexes for arrays
  • access to resources that are currently unavailable (missing files, ...)
  • missing classes on the classpath (at runtime)

("" , )

+9

; , - , API. , , , . .

Java . , Java Puzzlers, : , - / , .

Java:

  • Generics (Eclipse javac )
  • ( JLS)

+2

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

0
source

Source: https://habr.com/ru/post/1753162/


All Articles