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 546615
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T10:53:08+00:00 2026-05-13T10:53:08+00:00

I think Exception.fillInStackTrace should return Exception or derived Exception objects. Considering the two functions

  • 0

I think Exception.fillInStackTrace should return Exception or derived Exception objects. Considering the two functions below,

public static void f() throws Throwable {
    try {
        throw new Throwable();
    } catch (Exception e) {
        System.out.println("catch exception e");
        e.printStackTrace();
    } 
}
public static void g() throws Throwable {
    try {
        try {
            throw new Exception("exception");
        } catch (Exception e) {
            System.out.println("inner exception handler");
            throw e.fillInStackTrace();
        }
    } catch (Exception e) {
        System.out.println("outer exception handler");
        e.printStackTrace();
    }
}
  1. The exception handler could not catch the new Throwable() in the first function f().
  2. The exception handler could catch the e.fillInstackTrace() in the second function g().
  3. But the second function g() would still need to throws Throwable. This is really strange, since we could catch e.fillInstackTrace().

So my question is why doesn’t Exception.fillInStackTrace return Exception or Exception-derived instead of developing such a strange syntax?

EDIT:
To clarify my question: What I mean by “strange syntax” are

  1. Since Exception.fillInStackTrace() return Throwable reference,the exception handler which recieve Exception reference should not be able to catch the exception.Because java does not allow implict downcast,it should be something like return (Exception)e.fillInstackTrace().
  2. Since it is designed that the exception handler recieving Exception reference could handle the Throwable exception,there is no need to mark the method g() throws Throwable exception.But java compiler would enforce us to do so.

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-05-13T10:53:09+00:00Added an answer on May 13, 2026 at 10:53 am

    It’s actually easier to answer your questions starting with question 2.

    You asked:
    2. Since it is designed that the exception handler recieving Exception reference could handle the Throwable exception,there is no need to mark the method g() throws Throwable exception.But java compiler would enforce us to do so.

    Answer:
    Actually, catch( Exception e) cannot catch a Throwable.
    Try this:

    try {
           Throwable t = new Throwable();
           throw t.fillInStackTrace();
    } catch (Exception e) {
        System.out.println("outer exception handler");
        e.printStackTrace();
    } 
    

    You’ll see that the catch clause does not catch the throw in this case.

    The reason that the catch clause works in your g() method is that when you invoke throw e.fillInStackTrace(), the call to fillInStackTrace actually returns an Exception (that’s because e is an Exception itself). Since Exception is a subclass of Throwable, that does not contradict the declaration of fillInStackTrace.

    Now on to the first question

    You asked:
    1. Since Exception.fillInStackTrace() return Throwable reference,the exception handler which recieve Exception reference should not be able to catch the exception.Because java does not allow implict downcast,it should be something like return (Exception)e.fillInstackTrace().

    Answer:
    This is not exactly an implicit downcast. Think of this as a variation of overloading.

    Let’s say you have

    void process(Throwable t){
       ...
    }
    void process(Exception e){
      ...
    } 
    

    If you call process(someObject), it will be determined at runtime whether the first or the second process method gets called. Similarly, whether or not the catch(Exception e) clause can catch your throw will be determined at runtime, based on whether you throw an Exception or Throwable.

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

Sidebar

Ask A Question

Stats

  • Questions 292k
  • Answers 292k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Here is my full solution, with an IPython magic function.… May 13, 2026 at 6:18 pm
  • Editorial Team
    Editorial Team added an answer The event handler will get called once for each time… May 13, 2026 at 6:18 pm
  • Editorial Team
    Editorial Team added an answer Try the following Func<string,IEnumerable<Guid>> lambda = filter => ( _map… May 13, 2026 at 6:18 pm

Related Questions

Often, I found OutOfMemoryException on IBM Websphere Application Server. I think this exception occur
I am using VSTS 2008 + C# + .Net 3.5 to develop a Windows
I'm writing yet another ActiveRecord implementation for a company that is less scared of
I have some IO code that reads a stream within a try..catch. It catches
I've got a WebBrowser control on my page. To fire some javascript on a

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.