Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

compiler and run time errors

 
viswanadha Nirmala
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all,
Regarding SCJP-certification, how do we know whether the particular error is a run time error or a compiler error.Like are there any tips to find it.
Thanking you,
Nirmala.
 
Shivaji Marathe
Ranch Hand
Posts: 203
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You just have to write a lot of java programs and be familiar with what the compiler can and will check.
Also read through the hierarchy of classes derived from the Exceptions class
 
Greg Brouelette
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, a compile time error is something that does not match the API. So this would be a compile time error:
Vector v = new Vector();
v.AddElement("foo");
Notice the upper case "A" in AddElement. In the Java API all methods start with lower case letters, only Class names start with upper case. Therefore, this would not compile.
However, run time errors are also known as "logic errors". They are errors where you THINK you coded your program correctly ,and it does compile, but the logic doesn't do what you thought it would.
Example. Let's say I want to bet $50 if my poker hand is a straighflush and $10 if it's just a straight. And I want to bet $5 on all other hands. I may have code like this: (assume all the methods actually exists and all the data types are correct)

OK, that LOOKS right. But the evil bit here is that I didn't use curly braces to delimit my if statements. So even though it LOOKS like my else statement is attached to the if(type >= hand.STRAIGHT) line, it's ACTUALLY attached to the most recent if statment. Which in this case is the if (type >= hand.STRAIGHT_FLUSH) line.
In other words, this is what I THOUGHT I wrote:


But this is what I actually wrote:

That would be an example of a run time error because the code is valid, it will compile, but it doesn't do what you thought the code should do.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic