Page 1 of 1
Re: About Question enthuware.ocajp.i.v7.2.1210 :
Posted: Tue Dec 18, 2012 2:08 pm
by ksnortum
Here is how I "tested" the code:
Code: Select all
import java.io.IOException;
import java.sql.SQLException;
public class TryTest {
@SuppressWarnings("finally")
static void m1() throws Exception {
try {
double rand = Math.random();
if (rand < 0.3) {
System.out.println("Throwing IOException");
throw new IOException();
} else if (rand >= 0.3 && rand < 0.6) {
System.out.println("Throwing SQLException");
throw new SQLException();
} else {
System.out.println("No error thrown");
}
} catch (IOException e) {
throw new SQLException();
} catch (SQLException e) {
throw new InstantiationException();
} finally {
throw new CloneNotSupportedException();
}
}
public static void main(String[] args) throws Exception {
m1();
}
}
As I ran it repeatedly, it would print either Throwing some error or No error, but it would always through a CloneNotSupportedException at the end.
Re: About Question enthuware.ocajp.i.v7.2.1210 :
Posted: Tue Jul 09, 2013 7:34 pm
by ewebxml
For Explanation 2, it says
"So, if IOException is thrown at line 1, the control goes to first catch which throws SQLException.
Now, although there is a catch for SQLException, it won't catch the exception because it is at the same level."
_________
Should this explanation be interpreted in the following manner:
The superclass of SQLException is
java.lang.Object
|
+----java.lang.Throwable
|
+----java.lang.Exception
|
+----java.sql.SQLException
Superclass is java.lang.Exception.
-----
java.lang.Exception is also the superclass of IOException.
Since IOException and SQLException are both subclasses of java.lang.Exception,
can one say that they are both at the same level?
Question: Is this how the sentence 2 in the explanation should be interpreted?
-----
Please confirm.
Re: About Question enthuware.ocajp.i.v7.2.1210 :
Posted: Tue Jul 09, 2013 8:32 pm
by admin
No, the level is the nesting level of the catch blocks. For example:
Code: Select all
try{
}
catch(Exception11 e1){ //level 1
//exception thrown here cannot be caught by any catch block at level 1
try{
}catch(Exception21 e2){ //level 2
}
}
catch(Exception12 e1){//level 1
}
Re: About Question enthuware.ocajp.i.v7.2.1210 :
Posted: Thu Jan 08, 2015 1:28 pm
by zhengye1
Code: Select all
package oca;
import java.io.IOException;
import java.sql.SQLException;
public class ExceptionTest {
public static void main(String[] args) throws Exception{
m1();
}
@SuppressWarnings("finally")
public static void m1() throws Exception{
try{
throw new IOException();
}
catch(IOException e){
System.out.println("Catch IO Exception");
throw new SQLException();
}
catch(SQLException e){
System.out.println("Catch SQL Exception");
throw new InstantiationException();
}
finally{
System.out.println("Final block");
throw new CloneNotSupportedException();
}
}
}
This is what I try in Eclipse, but I receive
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Unreachable catch block for SQLException. This exception is never thrown from the try statement body
Why...
Re: About Question enthuware.ocajp.i.v7.2.1210 :
Posted: Thu Jan 08, 2015 9:12 pm
by admin
Please read my post above about the levels. You have a catch block for SQLException, but it can never be thrown by your try block. So the compiler realizes that the catch block for SQLException is useless here and therefore complains.
Re: About Question enthuware.ocajp.i.v7.2.1210 :
Posted: Fri Dec 11, 2015 6:20 am
by liviu_varlam
The code from the question is not compiling. If in the try block I throw an IOException, the catch(SQLException) line says that the exception is never thrown in body of corresponding try statement. If I throw an SQLExpception instead, then the catch(IOEXception) has the same compile error.
So this code will never compile in any case of the answers. How can any of the answers be correct then and reach the final clause?
Code: Select all
class MyException extends Exception {}
public class TestClass{
public static void main(String[] args) throws Exception{
TestClass tc = new TestClass();
tc.m1();
}
void m1() throws Exception{
try{
throw new IOException(); //line 1
}
catch (IOException e){
throw new SQLException();
}
catch(SQLException e){
throw new InstantiationException();
}
finally{
throw new CloneNotSupportedException(); // this is not a RuntimeException.
}
}
Re: About Question enthuware.ocajp.i.v7.2.1210 :
Posted: Sat Dec 12, 2015 9:50 am
by admin
Code for line 1 is not given. You have to assume that there is a valid line of code at that position that can potentially throw any exception that is referred to in the problem statement.
For example, you could have a call to method someMethod() that has throws IOException as well as SQLException in its throws clause.
Code: Select all
void someMethod() throws IOException, SQLException{
}
void m1() throws Exception{
try{
someMethod(); //line 1
}
}
Re: About Question enthuware.ocajp.i.v7.2.1210 :
Posted: Mon Dec 16, 2024 11:39 pm
by raphaelzintec
thanks for the last answear i forgot we need to realise that code must work... because i got this same opinion about exception never thrown