Page 1 of 1
About Question enthuware.ocajp.i.v7.2.1301 :
Posted: Wed Nov 20, 2013 12:13 am
by javanaut
How is Throwable a checked exception?
I thought checked-exceptions were only exceptions that extend java.lang.Exception but do not extend java.lang.RuntimeException.
Re: About Question enthuware.ocajp.i.v7.2.1301 :
Posted: Wed Nov 20, 2013 12:20 am
by javanaut
I just found the answer to my own question in the java API.
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.
Re: About Question enthuware.ocajp.i.v7.2.1301 :
Posted: Sun Jan 31, 2016 8:03 pm
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
Re: About Question enthuware.ocajp.i.v7.2.1301 :
Posted: Mon Nov 28, 2016 1:30 am
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();
}
}
}
Re: About Question enthuware.ocajp.i.v7.2.1301 :
Posted: Mon Nov 28, 2016 1:52 am
by admin
The exception thrown by the catch block is "suppressed". See this nice discussion on stacktrace:
http://stackoverflow.com/questions/7849 ... -exception
Re: About Question enthuware.ocajp.i.v7.2.1301 :
Posted: Wed Mar 08, 2017 11:05 am
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)
And this is OK (unchecked behavior?)
But this is not (checked behavior):
Code: Select all
try {} catch (java.io.IOException e) {}
Re: About Question enthuware.ocajp.i.v7.2.1301 :
Posted: Wed Mar 08, 2017 11:22 am
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.
Re: About Question enthuware.ocajp.i.v7.2.1301 :
Posted: Thu Jul 19, 2018 1:42 pm
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 ?
Re: About Question enthuware.ocajp.i.v7.2.1301 :
Posted: Thu Jul 19, 2018 8:45 pm
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.
Re: About Question enthuware.ocajp.i.v7.2.1301 :
Posted: Wed Jan 09, 2019 4:21 pm
by crazymind
Hi, it prints "Done" but should follow by an Exception in stack trace?
Re: About Question enthuware.ocajp.i.v7.2.1301 :
Posted: Wed Jan 09, 2019 7:41 pm
by admin
not sure what you mean. try running the code please.
Re: About Question enthuware.ocajp.i.v7.2.1301 :
Posted: Wed Jan 09, 2019 10:19 pm
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.
Re: About Question enthuware.ocajp.i.v7.2.1301 :
Posted: Sun Aug 04, 2019 3:39 am
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???
}
Re: About Question enthuware.ocajp.i.v7.2.1301 :
Posted: Sun Aug 04, 2019 8:05 am
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.
Re: About Question enthuware.ocajp.i.v7.2.1301 :
Posted: Mon Jan 25, 2021 6:34 am
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).