• Post Reply Bookmark Topic Watch Topic
  • New Topic

How can I make difference between Compilation fails and Runtime error?  RSS feed

 
Prashant Mumbarkar
Greenhorn
Posts: 27
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to know if there is any general rule/pattern about things which give compilation fails and things which go for Runtime error/exception. OR only way is to remember all of them and there is no actual pattern in it.

 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not sure what kind of pattern you're looking for - compile-time exceptions happen if the code can't be compiled. Reasons can be wrong syntax, typos in field/method/class names, missing classes/libraries in the classpath, etc. Runtime exceptions happen if something goes wrong at runtime (that probably doesn't help you).

Maybe if you told us what prompted this question we could give a better answer.
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think you're going to find a pattern. Compiletime errors happen when you write something that is not valid Java. You could read the Java Language Specification to get the complete description of Java syntax, but it's not an easy read. Just compile your code, the compiler will be better at spotting syntax errors than you are.

Runtime errors could be defined in several ways. A narrow definition could be that a runtime error is anything that results in an exception (nullpointer, dividing by zero etc). A better description would be anything your code does that it should not do, or anything your code does not do that it should do. There is no point writing syntactically correct code that does not throw exceptions if it does not do what it was meant to.

In either case, the way to detect runtime errors is to test your code.
 
Prashant Mumbarkar
Greenhorn
Posts: 27
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike. J. Thompson wrote:I don't think you're going to find a pattern. Compiletime errors happen when you write something that is not valid Java. You could read the Java Language Specification to get the complete description of Java syntax, but it's not an easy read. Just compile your code, the compiler will be better at spotting syntax errors than you are.

Runtime errors could be defined in several ways. A narrow definition could be that a runtime error is anything that results in an exception (nullpointer, dividing by zero etc). A better description would be anything your code does that it should not do, or anything your code does not do that it should do. There is no point writing syntactically correct code that does not throw exceptions if it does not do what it was meant to.

In either case, the way to detect runtime errors is to test your code.


Thanks Mike. I agree that best way to identify these by compiling or running the code. However my worry is with SCJP exam ..how to know about compilation fails and runtime error because there I won't be getting chance to compiling or running. That's why I was wondering if there is pattern or not.
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You will have to "compile" the code in your head in that case. Lots of practical programming experience will help greatly with that, making it much easier to spot things that don't compile.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!