Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Error detection in Java

 
matrix
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How can one predict whether an error in code is detected during Compilation or during run time.
Are there any rules of thumb or any guidelines defining as to when an error is detected?
Please explain this, are there any articles/resources discussing this in detail?
Thanks in Advance.
 
Travis Benning
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All errors that appear when you use the compiler are Compilation problems.
If you compile correctly, and then run your program, and either you get a runtime, or logic errors, those would be Runtime errors (and logic errors i guess)
Use try/catch blocks to catch and possibly throw your own exceptions.
Otherwise, Compilation errors have to be fixed with trial and error i think.
Ranch Hands, correct me if I'm wrong please
Hope this helped a little
TB
 
matrix
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Travis,
But by looking at the code, even before compiling, how can one make out where an error will be detected? I came across some questions where in certain piece of code is given and one has to predict whether the errors are detected during compilation or during runtime.
Keerthi
 
Dave Vick
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
matrix
Welcome to the Java Ranch, we hope you’ll enjoy visiting as a regular however,
your name is not in keeping with our naming policy here at the ranch. Please re-register under an appropriate name as shown in the policy.
Thanks again and we hope to see you around the ranch!!
 
Travis Benning
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
matrix,
one easy way to predict is to see if any of your numberic formula are ever divided by zero.
If you find one, it can throw an ArithmeticException exception. By enclosing the formula in a try/catch block, you can take care of the error ahead of time, in case it happens.
Also, if any of your variables haven't been initialized, they might throw an exception (NullPointerException maybe. not sure),
If any of your arrays go past there limits
(array[] with 2 objects, but you try and call the object located at array[2], which doesn't exist), it could throw ArrayIndexOutOfBounds exception, i think.
Hope this helps
TB
 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The essence of your question is what the SCJP test is all about! It's really a matter of learning the java language, syntax, and grammar, and writing a lot of sample code, and becomming familiar with how things. work.
It's a matter of experience. The more you work with java, the more you can predict the kinds of things you are talking about.
Rob
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic