• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

runtime vs compiler error

 
Maria Garcia
Ranch Hand
Posts: 86
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all,
i am still confused on whether a code generates a compiler or a run time error.
when should an error be caught by the compiler ? how should i know if an error would be caught during run time ?
thanks,
maria
 
Kumar Kausikasa
Ranch Hand
Posts: 45
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,
I do not know whether i understand your question right or not. But this is the general idea:
Compiler Error is generated mainly when the syntax you have written is not allowed in the java language. It means java compiler is not able to genrate a .class file becuase it doe snot understand what you have written.
Runtime Error: you get Runtime errors when you actually run the program. A good example is you have written a socket program to connect a machine.If that machine is not up and runnig, then it will throw a runtime excpetion.
HTH
Praveen.
 
Maria Garcia
Ranch Hand
Posts: 86
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks praveen
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Runtime Error: you get Runtime errors when you actually run the program. A good example is you have written a socket program to connect a machine.If that machine is not up and runnig, then it will throw a runtime excpetion.
Be aware of the terminology you are using, since a Socket will throw an IOException (a checked exception) if the bind operation fails. An exception thrown at runtime is not necessarly an instance of RuntimeException.
It would be better to write:
"If that machine is not up and running, then it will throw an exception at runtime", instead of a runtime exception
[ August 25, 2002: Message edited by: Valentin Crettaz ]
 
Paul Villangca
Ranch Hand
Posts: 133
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, it's a good idea to differentiate between errors and exceptions. Errors are events that, in general, are caused be problems beyond the control of the programmer, and can't be controlled (for example, bad sectors or some other unfortunate occurrence.) Exceptions, on the other hand, are (in general) problems in the code itself (like accessing array[array.length]), or eventualities that the compiler forces you to handle(i.e. IOException.)
 
Dmitry Golynkin
Greenhorn
Posts: 23
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
code:
__________________________________________________
Question 5
class D {
{System.out.print("1");}
static {System.out.print("2");}
D() {System.out.print("3");}
D(String s) {System.out.print("4");}
}
class E extends D {
{System.out.print("5");}
static {System.out.print("6");}
E() {System.out.print("7");this("e");}
E(String s) {System.out.print("8");}
}
class F {
public static void main(String[] args) {
new E();
}
}
compiler error
__________________________________________________
Question 6
class Q {
int i = 1;
{System.out.print("1");}
static {System.out.print("2");}
Q() {System.out.print("3");}
Q(int x) {System.out.print("4");}
}
class R extends Q {
int j = 2;
{System.out.print("5");}
static {System.out.print("6");}
R() {super(j);System.out.print("7");}
}
class S {
public static void main(String[] args) {
new R();
}
}
compiler error
__________________________________________________
I've seen no problem with Q5 - compiler could see that |this| stands on the second place. But in Q6 compiler doesn't know that |j| can't reached before R() construction. Why compiler error???
 
Barkat Mardhani
Ranch Hand
Posts: 787
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Dmitry:
The sub-class's instance variable are not available until super-class constructor is exectuted. Here is code whose output is 123456, to show what is done (available) when..
 
Dmitry Golynkin
Greenhorn
Posts: 23
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The output is 123546
(JBuilder 5 EE)
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic