• Post Reply Bookmark Topic Watch Topic
  • New Topic

Runtime vs compiler error  RSS feed

 
Bora Sabrioglu
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi folks,

are there some rules / guidelines /clues for determining if the compiler will catch the error or if the JVM will blow up at runtime - or do we have to learn it all by heart (if something is a compiler error or runtime error)?

For example, if I didn't read about it I would have guessed this as a compiler error, but it is a runtime error:


This throws an ArrayIndexOutOfBoundsException... which is a runtime exception.

But I would have guessed that the compiler "sees" that x[5] is out of bounds, since it was declared as new int[5]...

Would be great if there were some clues / rules how we can judge if something will blow up at runtime or will be catched at compile time... just to make life easier for us... otherwise we would have to learn everything by heart...

Thanks in advance.
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If a method throws a checked Exception then your code will not compile unless you either surround the method call in a try/catch construct, or your method also declares it throws the Exception.

I think you're over-estimating what the compiler will do for you. It will not fail compilation just because your code will throw an Exception at runtime. It will only fail compilation if your code is not syntactically correct.

Perhaps your confusion comes from the fact unchecked Exceptions extend RuntimeException. This does not mean that checked Exceptions are compile-time exceptions.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bora Sabrioglu wrote:Would be great if there were some clues / rules how we can judge if something will blow up at runtime or will be catched at compile time... just to make life easier for us... otherwise we would have to learn everything by heart...

There are, and you've broken the former. As you probably know, Java array indexes are 0-based.

Yes, it's possible that in your particular case the compiler could throw the exception but, since accessing arrays with literal indexes (except possibly 0) isn't a particularly common thing to do, it seems silly to add code to the compiler to check for it. And there's no earthly reason why the compiler should throw a different exception simply because it's not being thrown at runtime.

My 2¢.

Winston
 
Bora Sabrioglu
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike. J. Thompson wrote:I think you're over-estimating what the compiler will do for you.

I think so too, yes... but hey, at least I don't expect my compiler to make some coffee for me
It will only fail compilation if your code is not syntactically correct.

It think I can use this as a general rule-of-thumb for the future:
It doesnt compile if its syntactically incorrect. (semicolon, brace, brackets, etc. missing, misspelled keyword, you name it...)
It will compile but fail at runtime if it is semantically incorrect (like e.g. a negative number as an array index).
This does not mean that checked Exceptions are compile-time exceptions.

True. Like for example, if I declare a RemoteException (extends Exception, thus checked) it doesn't mean that it is thrown at compile time.... if the server is down, the network broken, the network-printer offline, you name it, it is thrown at runtime....

Thanks everyone for clearing things up and throwing in new ideas!
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bora Sabrioglu wrote:It doesnt compile if its syntactically incorrect. (semicolon, brace, brackets, etc. missing, misspelled keyword, you name it...)

True. However, the compiler (especially since the advent of generics) is actually quite smart at figuring out what are and are not "syntactic" problems. Just one case in point being the rules for "unreachable code".

Winston
 
Stevens Miller
Bartender
Posts: 1445
30
C++ Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bora Sabrioglu wrote:

This throws an ArrayIndexOutOfBoundsException... which is a runtime exception.

But I would have guessed that the compiler "sees" that x[5] is out of bounds, since it was declared as new int[5]...


While it would be a form of lunacy to do so, those two lines might appear inside a multi-threaded method, one in which x is enlarged elsewhere. In that case, Line 2 wouldn't cause a run-time error if it executed on one thread after x had been enlarged on another thread.
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is quite possible that you could write this sort of thing:That is code which is both syntactically and semantically correct. It would be really difficult to program a compiler to recognise that sort of code. It would have to execute the entire code to be able to check that sort of thing. And that would be difficult enough to verify without SM's threading.
What if you wrote this sort of thing?How is the compiler to know that the user will enter a number over 5?
Now you see, there are some things which the compiler cannot be expected to predict e.g. whether there will be such an Exception. So there is no point in trying to program that sort of prediction into a compiler. So you don't do it.
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote: It would have to execute the entire code to be able to check that sort of thing. And that would be difficult enough to verify without SM's threading.


And in the general case I doubt it would even be possible because it would probably need to solve the Halting Problem to do that, which is impossible.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!