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.
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
Sun Certified Programmer for Java 2 Platform
Joined: Jan 21, 2002
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
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!!
Joined: Jan 24, 2002
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, which doesn't exist), it could throw ArrayIndexOutOfBounds exception, i think. Hope this helps TB
The essence of your question is what the SCJPtest 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