About Question enthuware.ocajp.i.v7.2.889 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
Kevin30
Posts: 28
Joined: Sun Oct 25, 2015 10:14 am
Contact:

About Question enthuware.ocajp.i.v7.2.889 :

Post by Kevin30 »

The code is:

Code: Select all

1. class A{
2.    String value = "test";
3.    A(String val){
4.        this.value = val;
5.     }
6. }
7. public class TestClass {
8.     public static void main(String[] args) throws Exception {
9.         new A("new test").print();
10.    }
11.}
I chose the option: "It will throw an exception at run time".

Why? Because of two rules I learned (based on Sierra & Bates' book):
A) If a method invokes a checked exception (from the Exception-class), this exception must either be handled (using try-catch) or declared (using throws in its method). If neither happens, a compiler error will follow.
B) An uncaught exception will propagate through the call stack until the exception is either caught, or the exception reaches the main()-method. If main() also "ducks" the exception by declaring it (using throws) the JVM will shut down and a stack trace will be printed.

Now, in this code a checked exception will be thrown at line 9. This exception will then be declared using throws in the main()-method. Therefore, the main()-method "ducks" the exception, and ultimately the JVM will shut down and a stack trace will be printed (but no compiler error).

Am I incorrect? Is a JVM shutdown and a printout of the stack trace the same as a compiler error?

admin
Site Admin
Posts: 10388
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.889 :

Post by admin »

>Now, in this code a checked exception will be thrown at line 9.
Why do you think so?

Kevin30
Posts: 28
Joined: Sun Oct 25, 2015 10:14 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.889 :

Post by Kevin30 »

admin wrote:>Now, in this code a checked exception will be thrown at line 9.
Why do you think so?
Well, in learning Java-theory and in differentiating between checked exception and unchecked exceptions I learned - or at least I thought I learned - that compilation errors are checked exceptions, because:
"Something is called a CHECKED exception because the COMPILER checks to make sure that they are handled or declared." If it isn't handled or declared, it is a compiler error.

line 9 is:

Code: Select all

9.         new A("new test").print();
print(); doesn't exist and is therefore a compiler error. So therefore it is a checked exception.

But I am beginning to sense that this reasoning may not be correct?

admin
Site Admin
Posts: 10388
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.889 :

Post by admin »

The reason I asked you that question is to find out exactly where are you going wrong :)
You reasoning about the JVM and checked exceptions is correct. But how are you going to run the given code, if it is not going to compile? JVM doesn't execute java files. It executes class files. How are you going to get a class file if the compiler doesn't generate one?

Kevin30
Posts: 28
Joined: Sun Oct 25, 2015 10:14 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.889 :

Post by Kevin30 »

Yes, I see now what you mean. Thank you!

I guess that using an Integrated Development Environment (IDE) to test out little programs has its own drawbacks, and I just now demonstrated one of these. If I were to only use the command line and no IDE in studying for the exam this would have been more clear to me earlier. But only using the command line does make it extra hard.

So can I say that if you have a checked exception in the code this is always a compiler error, but that if you have a compiler error in the code this is NOT always a checked exception?

Thanks again!

admin
Site Admin
Posts: 10388
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.889 :

Post by admin »

Well, if there is a compilation error, there is not going to be a class file. And if there is no class file, there is nothing to execute. If there is no execution, there cannot be any exception or an output.

You should not be using an IDE while studying for this exam.

Post Reply

Who is online

Users browsing this forum: No registered users and 12 guests