About Question enthuware.ocajp.i.v8.2.826 :

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

Moderator: admin

Post Reply
corptrainer2

About Question enthuware.ocajp.i.v8.2.826 :

Post by corptrainer2 »

A student submitted the following:

Please see attached files.

Question answer is incorrect. The answered marked correct is a stack trace. The code shows a sysout print statement, which will NOT print a stack trace. (Type the code in and execute it). The correct answer is C, which only prints the exception and the message.
http://screencast.com/t/VWbZiK2L
http://screencast.com/t/OlHFPO0Re

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

Re: About Question enthuware.ocajp.i.v8.2.826 :

Post by admin »

The given answer and explanation are correct. The question does not ask what the print statement prints. The question asks, "What will be the output when the following program is run?"

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

Sergey
Posts: 39
Joined: Sat Jul 29, 2017 1:04 pm
Contact:

Re: About Question enthuware.ocajp.i.v8.2.826 :

Post by Sergey »

ArrayIndexOutOfBoundsException will be thrown, which cannot be caught by catch(MyException ) clause.
Why?
Java.lang.RuntimeException extends java.lang.Exception.

Am i correct? That is because of:

Code: Select all

class MyException extends Exception {

    public MyException(String msg){ super(msg);}
}
It means another branch of exceptions and there is no way to catch this runtime exception.

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

Re: About Question enthuware.ocajp.i.v8.2.826 :

Post by admin »

The exception that is thrown in the try block must satisfy the "is-a" check with the exception declared in the catch clause for it to be caught. So if the code throws ArrayIndexOutOfBoundsException and if your catch clause is catch(MyException re), you have to check whether "ArrayIndexOutOfBoundsException is-a MyException"? The answer is no, ArrayIndexOutOfBoundsException is not a MyException. Therefore, an ArrayIndexOutOfBoundsException cannot be caught by catch(MyException me).

If your catch clause was catch(RuntimeException re), then yes, it would have beeb possible because ArrayIndexOutOfBoundsException is a RuntimeException.
If you like our products and services, please help us by posting your review here.

flex567
Posts: 202
Joined: Mon Apr 02, 2018 8:40 am
Contact:

Re: About Question enthuware.ocajp.i.v8.2.826 :

Post by flex567 »

From the answer:
Note that there are a few questions in the exam that test your knowledge about how exception messages are printed.

When you use System.out.println(exception), a stack trace is not printed. Just the name of the exception class and the message is printed.

When you use exception.printStackTrace(), a complete chain of the names of the methods called, along with the line numbers, is printed. It contains the names of the methods in the chain of method calls that led to the place where the exception was created going back up to the point where the thread, in which the exception was created, was started.
You explained what happens if you use one of the options but which option is used in this program?

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

Re: About Question enthuware.ocajp.i.v8.2.826 :

Post by admin »

The exception stack trace printed by the given program is actually printed by the JVM and not by the program itself because the exception is not being caught by this program.
If you like our products and services, please help us by posting your review here.

Leonid
Posts: 9
Joined: Mon Jun 28, 2021 4:50 am
Contact:

Re: About Question enthuware.ocajp.i.v8.2.826 :

Post by Leonid »

I read somewhere, if any exceptions throws in static block or static method then jvm throw error instead exceptions. So I choose answer B instead of A. Please correct me.

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

Re: About Question enthuware.ocajp.i.v8.2.826 :

Post by admin »

static block and static method are two different things. If an uncaught exception is thrown from a static block then yes, it is handled by the jvm and it wraps the exception and throws an ExceptionInInitializationErrror.
But that is not the situation here.
If you like our products and services, please help us by posting your review here.

Leonid
Posts: 9
Joined: Mon Jun 28, 2021 4:50 am
Contact:

Re: About Question enthuware.ocajp.i.v8.2.826 :

Post by Leonid »

Thank you, I imagined this rule accepted in this case too.

Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 24 guests