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

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

Moderator: admin

Post Reply
ksnortum
Posts: 30
Joined: Fri Dec 07, 2012 6:09 pm
Contact:

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

Post 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.

ewebxml
Posts: 77
Joined: Sun Jun 30, 2013 10:04 pm
Contact:

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

Post 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.

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

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

Post 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
       
   }

zhengye1
Posts: 17
Joined: Wed Jan 07, 2015 12:06 am
Contact:

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

Post 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...

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

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

Post 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.

liviu_varlam
Posts: 3
Joined: Fri Dec 11, 2015 6:06 am
Contact:

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

Post 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.
   }
}

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

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

Post 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
   }
}

raphaelzintec
Posts: 167
Joined: Sun Apr 21, 2024 10:43 am
Contact:

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

Post 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

Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 17 guests