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

  • SEARCH
  • Home
  • 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 8348651
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T07:45:26+00:00 2026-06-09T07:45:26+00:00

So I have this relevant code… public class PokemonTrainer { private Pokemon p =

  • 0

So I have this relevant code…

public class PokemonTrainer {
    private Pokemon p = new Squirtle();
    private String name;

    public PokemonTrainer(String name) {
        this.name = name;
    }

    public static void main(String[] args) {
        PokemonTrainer pt = new PokemonTrainer("Ash");
        try {pt.fightGary();}

        catch (Charmander c) {
            System.out.println("You were roasted by a Charmander.");
        }

        catch (Squirtle s) {
            System.out.println("You were drowned by a Squirtle.");
        }

        catch (Bulbasaur b) {
            System.out.println("You were strangled by a Bulbasaur.");
        }

        catch (Pokemon p) {
            System.out.println("You survived!");
        }
    }

    public void fightGary() throws Pokemon {
        throw p;
    }

public class Pokemon extends Exception {}
public class Bulbasaur extends Pokemon {}
public class Squirtle extends Pokemon {}
public class Charmander extends Pokemon {}

Why is it that this prints “You were drowned by a Squirtle”?

In my reasoning, “catch” is a method, and when an object is passed into a method, the method evaluates based on the object’s STATIC TYPE — that is, “Pokemon”, in this case — this is demonstrated in the below short example:

public class PokemonTrainer {
    private Pokemon p = new Squirtle();
    private String name;

    public PokemonTrainer(String name) {
        this.name = name;
    }

    public static void main(String[] args) {
        PokemonTrainer pt = new PokemonTrainer("Ash");
        pt.fightGary(pt.p); // ------------ Prints "Pokemon!!!"
    }

    public void fightGary(Pokemon p) {
        System.out.println("Pokemon!!!");
    }

    public void fightGary(Squirtle s) {
        System.out.println("Squirtle!!!");
    }
}

So what’s different about these two examples? Why does the first example print what it does?

Thanks!

  • 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-09T07:45:27+00:00Added an answer on June 9, 2026 at 7:45 am

    In my reasoning, “catch” is a method

    That’s the first mistake. catch isn’t a method, and trying to think of it as if it were is going to cause you problems.

    It’s a language construct with its own rules. You should read the language specification section 14.20 or the exceptions tutorial for more information.

    In short, when you have a sequence of catch blocks, the first one which matches the execution time type of the thrown exception is executed. (The compiler will prevent you from catching a more-general exception before a more-specific one.)

    So if you have:

    try {
      doSomething();
    } catch (FileNotFoundException e) {
      ...
    } catch (IOException e) {
      ...
    }
    

    then if doSomething() throws a FileNotFoundException it will execute the first catch block (and only that one) – whereas if it throws any other kinds of IOException, it will execute the second catch block. Any other exceptions will propagate up the stack.

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

Sidebar

Related Questions

Ok so i have this relevant code in jQuery: myself.insertAfter('.archive:last'); 'myself' is just a
I have something like this in my code: String boundary = thisIsMyBoundaryString; StringBuilder body
I'm developing a class and I have this structure: class userInfo { public $interval
Hey guys, I have this code within a function inside a class that is
I have a problem with my JDBC code. This is the relevant code: /**
This is the relevant code: throw new Exception(Message); And this is the error message
I have this script set up that echoes all relevant users in a database
I have 2 entities in CoreData (which are relevant to this). Let's call them
This is what I have in mind: Can anyone point out some relevant resources?
I have this string 2012-06-27 16:17:06 and I want to convert it to GMT

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.