Page 1 of 1

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

Posted: Thu Oct 13, 2016 3:31 am
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

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

Posted: Thu Oct 13, 2016 9:47 am
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.

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

Posted: Wed Oct 25, 2017 12:06 pm
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.

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

Posted: Wed Oct 25, 2017 8:33 pm
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.

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

Posted: Sun May 13, 2018 7:57 am
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?

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

Posted: Sun May 13, 2018 10:37 am
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.

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

Posted: Fri Aug 13, 2021 1:54 am
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.

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

Posted: Fri Aug 13, 2021 3:12 am
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.

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

Posted: Fri Aug 13, 2021 5:01 am
by Leonid
Thank you, I imagined this rule accepted in this case too.