• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

compile-time error & run-time error

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm always confused by these two features.Sometimes it's not so easy to differentiate them.Is there any tips to help?
 
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by bobbie Giant:
I'm always confused by these two features.Sometimes it's not so easy to differentiate them.Is there any tips to help?


The short answer is that the compile time errors are a result of running the javac program on some source (.java) file(s) that have one or more mistakes in language usage (missing import statements, typos, etc). The compiler can't translate the source file into a .class file due to the errors.
Run-time errors are generated from programs that are correctly written as far as the syntax of the language is concerned but have some error in logic (for example, attemping to use a reference to an object that has been set to null....
Is that where your question was heading?
 
bobbie Giant
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your reply.But I'm also not completely clear.
e.g.:
//
int[] n= new int[3];
n[-1]=3;
//
As array syntex concerned,the "-1" element is illegal.Why is it a run-time exception but not a compiler error.
 
Ranch Hand
Posts: 384
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Bobbie:
The compiler was only interested in the decleration of the array, and the compiler just looked for:
int[] n= new int[3]; //a number inside [] on the left side of the =, not there so this bit fine
//a number inside the [] on the right side, this needed or []{1,2,3} something like this so decleration is fine
n[-1]=3; //compiler see this as fine
but running it, this is when it checks for boundaries, so because the array does not fit into your boundary, it gives a runtime error of ArrayIndexOutOfBoundsException.
I hope this helps
Davy
 
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The compiler couldn't care less whether you used n[-1] or n[0].
It will translate both into <memorylocation>+offset with only the offset being different.
The value for the offset isn't tested at compiletime (except to check for type).
As the array index can be any integer type (and not just unsigned ints of which Java supports only char) the compiler can therefore not detect your illegal offset value.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic