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

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

Moderator: admin

Post Reply
javanaut
Posts: 22
Joined: Wed Aug 21, 2013 12:29 am
Contact:

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

Post by javanaut »

How is Throwable a checked exception? :shock:

I thought checked-exceptions were only exceptions that extend java.lang.Exception but do not extend java.lang.RuntimeException.

javanaut
Posts: 22
Joined: Wed Aug 21, 2013 12:29 am
Contact:

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

Post by javanaut »

I just found the answer to my own question in the java API. :D :ugeek:

This is from java.lang.Throwable API documentation - in the first paragraph:
For the purposes of compile-time checking of exceptions, Throwable and any subclass of Throwable that is not also a subclass of either RuntimeException or Error are regarded as checked exceptions.

chronix
Posts: 5
Joined: Mon Jan 04, 2016 12:28 am
Contact:

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

Post by chronix »

The words "There is nothing wrong with the code.." is a bit misleading as it sometimes
imply that it will compile and run with no trouble. Luckily no other answer seems correct... but
this question eats away a few minutes of your time looking for "Done.. followed by exception.."
or thinking that you might have got it wrong. cheers

mj.anjuthan
Posts: 10
Joined: Thu Nov 10, 2016 3:07 am
Contact:

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

Post by mj.anjuthan »

I notice that if catch block and finally block both throw different checked exceptions we have to declare only the exception throw by finally block in the method it is thrown. Is this the case always? What happens to the exception thrown by the catch block?

Eg:

Code: Select all

class DogEx extends Exception {}
class CatEx extends Exception {}

public class Foo{
	
	public static void main(String[] args) throws CatEx{
		
		try{
			throw new DogEx();
		}
		catch(DogEx e){
			throw new DogEx();
		}
		finally{
			throw new CatEx();
		}
    }

}

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

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

Post by admin »

The exception thrown by the catch block is "suppressed". See this nice discussion on stacktrace:
http://stackoverflow.com/questions/7849 ... -exception
If you like our products and services, please help us by posting your review here.

lenalena
Posts: 56
Joined: Tue Feb 21, 2017 4:24 pm
Contact:

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

Post by lenalena »

I'm a little vague on the Exception class... It seems it has a bit of it's own behavior, not fully consistent with checked or unchecked...

It behaves as a checked exception in the sense that it must be handled if it's thrown. However, it's OK to catch it even if it's not explicitly thrown - which I thought was a property of RuntimeException.
Example...
This has to be handled (checked behavior)

Code: Select all

void m1() throws Exception {} 


And this is OK (unchecked behavior?)

Code: Select all

try {} catch (Exception e) {}
But this is not (checked behavior):

Code: Select all

try {} catch (java.io.IOException e) {}

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

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

Post by admin »

It is because RuntimeException is a subclass of Exception and RuntimeException is an unchecked exception. Couple this with the fact that a catch block that catches a super class can always catch a subclass exception, there is no way for a compiler to reject try{ }catch(Exception e){ }.

Compiler has no option but to allow the possibility of a code block throwing an RTE without declaring (since it is unchecked) and since RTE can be caught using catch(Exception ), the compiler has to allow it. The same goes for catch(Throwable ) as well.

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

SeoaneR
Posts: 8
Joined: Tue Nov 07, 2017 8:23 am
Contact:

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

Post by SeoaneR »

I'm a bit confused with the catch clause. It's catching the exception but then it throws an exception.
Is this because of the throw e inside the catch clause?
What is the throw e actually doing ?

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

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

Post by admin »

The catch block catches the exception that is thrown by the code within the try block. The throw e; statement inside the catch block throws the same exception again. This is not caught by any catch block and so it propagates outside the method.
If you like our products and services, please help us by posting your review here.

crazymind
Posts: 85
Joined: Mon Dec 24, 2018 6:24 pm
Contact:

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

Post by crazymind »

Hi, it prints "Done" but should follow by an Exception in stack trace?

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

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

Post by admin »

not sure what you mean. try running the code please.
If you like our products and services, please help us by posting your review here.

crazymind
Posts: 85
Joined: Mon Dec 24, 2018 6:24 pm
Contact:

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

Post by crazymind »

admin wrote:
Wed Jan 09, 2019 7:41 pm
not sure what you mean. try running the code please.
The words "There is nothing wrong with the code.." is a bit misleading as it sometimes
imply that it will compile and run with no trouble. Luckily no other answer seems correct... but
this question eats away a few minutes of your time looking for "Done.. followed by exception.."
or thinking that you might have got it wrong. cheers
Thanks. I also find output prints Done followed by exception.

Mikhail Volkov
Posts: 8
Joined: Mon Jul 29, 2019 11:25 am
Contact:

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

Post by Mikhail Volkov »

about }catch(SomeThrowable e){

The same example:
try {...}
catch (Exception e) {
throw new Exception(); - Unhandled exception. I understand this.
but
throw e; - it is ok. Why???
}

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

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

Post by admin »

Could you please post complete and exact code with a little more detail on what you are asking? That will help anyone reading your question in arriving at a better answer.
If you like our products and services, please help us by posting your review here.

promtbvb
Posts: 3
Joined: Thu Jun 11, 2020 4:17 pm
Contact:

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

Post by promtbvb »

Mikhail Volkov wrote:
Sun Aug 04, 2019 3:39 am
about }catch(SomeThrowable e){

The same example:
try {...}
catch (Exception e) {
throw new Exception(); - Unhandled exception. I understand this.
but
throw e; - it is ok. Why???
}
throw e; => is in the method signature,and the calling method should handle (throw or catch);
throws a new exception (); => is not in the method signature, so it must be handled (throw or catch).

Post Reply

Who is online

Users browsing this forum: admin and 99 guests