Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9268557
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T14:50:43+00:00 2026-06-18T14:50:43+00:00

class X extends Exception { } class Y extends X { } class Z

  • 0
class X extends Exception {
}

class Y extends X {
}

class Z extends Y {
}

public class Test {

    static void aMethod() throws Z {
        throw new Z();
    }

    public static void main(String[] args){
    int x = 10;
    try { 
        aMethod(); 
    }
    catch(X e) { 
        System.out.println(“Error X”);
        }
    catch(Y e) { 
        System.out.println(“Error Y”);
        }
   }
}

What is the output?

(A) The exception will go uncaught by both catch
blocks

(B) It will print “Error X”

(C) It will print “Error Y”

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T14:50:44+00:00Added an answer on June 18, 2026 at 2:50 pm

    (D) It does not compile 😉

    The second catch block is unreachable.

    If you turn it around, and catch Y first you still should get a compiler warning, as Z is a checked exception and the compiler will detect that only the first catch clause can be reached. But the example will work, and “Error Y” will be printed.

    Java always evaluates the catch clauses in the order they are defined, which is the reason you should define the most specific exceptions first, in this case catch (Y y) should come before catch (X x).

    EDIT: In the first version I assumed that the compiler error “Unreachable code” can be turned off with special compiler settings. Thanks to Marko Topolnik for the clarification.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

public class NewTest extends SeleneseTestCase { public static Test suite() throws Exception { TestSuite
Given the following Java code: public class Test { static private class MyThread extends
i have a class extends asynctask private class taskMK extends AsyncTask<Void,Void,JSONObject>{ String url; JSONObject
import java.util.*; import java.io.*; import java.util.regex.*; class ZiggyTest2 extends Thread{ String sa; public ZiggyTest2(String
@Component public class AnnotationTest extends TestCase{ ServiceTest serviceTest; @Autowired(required=false) public void setServiceTest(ServiceTest serviceTest) {
I have the following classes: package models; public class Test extends activejdbc.Model { }
I wrote a simple implementation class: public interface MyRemote extends Remote { public String
So I recently wrote a java class that extends exception and used an instance
I'm confused by the following code: class MyException extends Exception {} class AnotherException extends
I want to extend RuntimeException to create this specific exception: class CompileLinkException extends RuntimeException

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.