Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 730
Michael Avatar asked Jul 05 '10 12:07

Michael


People also ask

How do you know if error is compile time or runtime?

A compile-time error generally refers to the errors that correspond to the semantics or syntax. A runtime error refers to the error that we encounter during the code execution during runtime. We can easily fix a compile-time error during the development of code. A compiler cannot identify a runtime error.

What is a compiler error in Java?

Compile-time errors occur when there are syntactical issues present in application code, for example, missing semicolons or parentheses, misspelled keywords or usage of undeclared variables. These syntax errors are detected by the Java compiler at compile-time and an error message is displayed on the screen.

Is exception is runtime error or compile time error?

An Exception is an event that occurs during the program execution and disrupts the normal flow of the program's execution. Errors mostly happen at runtime, excepts Syntax errors which prevent the code from running. Whereas an Exception can occur at runtime as well as compile-time.

Is error a runtime exception in Java?

Runtime errors occur when something goes wrong in the normal execution of a program. When severe enough, these errors abruptly terminate an application. To help programmers both anticipate and recover from runtime errors, the Java programming language defines a special class named the RuntimeException.


2 Answers

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)

like image 97
Andreas Dolk Avatar answered Sep 28 '22 03:09

Andreas Dolk


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

  • Is 1/0 a legal Java expression?
  • What is the difference between javac and the Eclipse compiler?
like image 36
polygenelubricants Avatar answered Sep 28 '22 03:09

polygenelubricants