About Question enthuware.ocajp.i.v8.2.826 :
Moderator: admin
About Question enthuware.ocajp.i.v8.2.826 :
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
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
-
- Site Admin
- Posts: 10046
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.826 :
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.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 39
- Joined: Sat Jul 29, 2017 1:04 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.826 :
Why?ArrayIndexOutOfBoundsException will be thrown, which cannot be caught by catch(MyException ) clause.
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);}
}
-
- Site Admin
- Posts: 10046
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.826 :
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 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.
-
- Posts: 202
- Joined: Mon Apr 02, 2018 8:40 am
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.826 :
From the answer:
You explained what happens if you use one of the options but which option is used in this program?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.
-
- Site Admin
- Posts: 10046
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.826 :
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.
-
- Posts: 9
- Joined: Mon Jun 28, 2021 4:50 am
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.826 :
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.
-
- Site Admin
- Posts: 10046
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.826 :
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.
But that is not the situation here.
If you like our products and services, please help us by posting your review here.
-
- Posts: 9
- Joined: Mon Jun 28, 2021 4:50 am
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.826 :
Thank you, I imagined this rule accepted in this case too.
Who is online
Users browsing this forum: No registered users and 72 guests