TestFin.java:7: exception java.io.IOException is never thrown in body of corresponding try statement
} catch (IOException e) {e.printStackTrace();}
^
1 error
Shawn Smith wrote:Because System.exit() can throw a SecurityException, which can be implicitly down cast to an Exception. Nothing in the body of the try throws an IOException.
OCPJP 6.
In Your Pursuit Towards Certification, NEVER Give Up.
Shawn Smith wrote:Since you can't have a try without a catch ...
Shawn Smith wrote:Since you can't have a try without a catch, and you can have an empty try block, you must allow the system to catch something.
Shawn Smith wrote:If you do have "one or more legal lines of code that could throw an exception", you can only catch either Exceptions declared in the throws clauses of those lines, or their base classes.
OCPJP 6
We can guess several things from this, like "its always possible to catch RuntimeException", and "Exception is the only exception to this rule because an Exception can potentially be a RuntimeException" and so on. But does anyone actually know what's going on here and why?
Shawn Smith wrote:Thanks for the debate, you folks are going to make me think real hard before typing
![]()
Shawn Smith wrote: Thanks for the debate, you folks are going to make me think real hard before typing
![]()
OCPJP 6.
In Your Pursuit Towards Certification, NEVER Give Up.
Ikpefua Jacob-Obinyan wrote:Hello Guys, This debate is very interesting!...@Andreas It looks like the puzzle continues!
Now observe how 'Exception' behaves in the following program:
Compilation FAILS at line 9 because the main method fails to comply with the decalre-or-handle obligation.
If you comment lines 3 and 9.....And uncomment lines 5 and 11 the code compiles and runs without problems.
Permit me add this question to yours: Why is 'Exception' behaving like 'non-checked' in a catch block and like
'checked' in a methods declaration?
For the purpose of the exam I think it is IMPORTANT to take note of this behaviour.
Shawn Smith wrote: Thanks for the debate, you folks are going to make me think real hard before typing
![]()
You are NOT alone bro, just like Andreas said, dont try to be perfect everyone here knows no one is above mistakes.
Ikpefua Jacob-Obinyan wrote:Hello Guys, This debate is very interesting!...@Andreas It looks like the puzzle continues!
Now observe how 'Exception' behaves in the following program:
Permit me add this question to yours: Why is 'Exception' behaving like 'non-checked' in a catch block and like
'checked' in a methods declaration?
For the purpose of the exam I think it is IMPORTANT to take note of this behaviour.
OCPJP6-05-11
"Your life is in your hands, to make of it what you choose."
OCPJP 6.
In Your Pursuit Towards Certification, NEVER Give Up.
Why is 'Exception' behaving like 'non-checked' when used in a 'catch' PARAMETER and like 'checked' when used in a 'methods' DECLARATION?
OCPJP6-05-11
"Your life is in your hands, to make of it what you choose."
OCPJP 6.
In Your Pursuit Towards Certification, NEVER Give Up.
S Thiyanesh wrote:The reason that "Exception" is allowed in the catch clause is "Any runtime exception is a subclass Exception" and we are handling the broader exception.
OCPJP 6.
In Your Pursuit Towards Certification, NEVER Give Up.
A method should throw an exception only if at least one of the following three criteria is met:
* The exception is an instance of RuntimeException or one of its subclasses.
* The exception is an instance of Error or one of its subclasses.
* The exception is an instance of one of the exception classes specified in the exception_index_table just described, or one of their subclasses.
These requirements are not enforced in the Java virtual machine; they are enforced only at compile time.
Compilation of try-catch constructs is straightforward. For example,
void catchOne() {
try {
tryItOut();
} catch (TestExc e) {
handleExc(e);
}
}
is compiled as
Method void catchOne()
0 aload_0 // Beginning of try block
1 invokevirtual #6 // Method Example.tryItOut()V
4 return // End of try block; normal return
5 astore_1 // Store thrown value in local variable 1
6 aload_0 // Push this
7 aload_1 // Push thrown value
8 invokevirtual #5 // Invoke handler method:
// Example.handleExc(LTestExc;)V
11 return // Return after handling TestExc
Exception table:
From To Target Type
0 4 5 Class TestExc
OCPJP6-05-11
"Your life is in your hands, to make of it what you choose."
Tommy wrote: Regards to this problem I've provided the reason why it give you a COMPILATION FAIL. I think you MISUNDERSTOOD the concept of exception being declared in methods and handle in try/catch block. Correct me if I'm wrong...
OCPJP 6.
In Your Pursuit Towards Certification, NEVER Give Up.
Ikpefua Jacob-Obinyan wrote:
Hello Tommy,
again I want to express my appreciation for your effort in explaining these concepts, however if you NOTICED the last statement/s I made you will see that I agree with you and understand the concepts very well.
I was ONLY wondering why one is NOT 'obliged' to provide code that may result in an exception being thrown, when 'Exception' is used in a 'catch' PARAMETER, and in effect 'obliged' to handle-or-declare by a methods CALLER when 'Exception' is added to a methods declaration.
I know everything that is REQUIRED about exception for the purpose of the exams AND I understand these concepts better THANKS to you all.
Regards
Ikpefua.
OCPJP6-05-11
"Your life is in your hands, to make of it what you choose."
Often the most important part of the news is what they didn't tell.
My honeysuckle is blooming this year! Now to fertilize this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
|