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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T01:04:34+00:00 2026-06-01T01:04:34+00:00

So I recently wrote a java class that extends exception and used an instance

  • 0

So I recently wrote a java class that extends exception and used an instance of this class to check cases and throw itself if an error occurs. I found out that when the caller of main catches this exception, the line it says the exception was thrown from is the line from where the exception was created, and not from where it was thrown. I’m just wondering why this is, and whether it is intended behavior for the jvm or not since this is not a common way of throwing exceptions. If it is intended behavior, what is the rational for this as it seems like the line number from where the exception was thrown would be more useful(and probably easier to track through the stack). Sample cases follow demonstrating expected behavior and unexpected.


Normal Exception throwing:

1  public class Test
2  {
3   public static void main(String ... args) throws Throwable
4   {
5       switch(5)
6       {
7           case 1: throw new Exception("Exception");
8           case 2: throw new Exception("Exception");
9           case 3: throw new Exception("Exception");
10          case 4: throw new Exception("Exception");
11          case 5: throw new Exception("Exception");
12      }
13  }
14 }

output:

Exception in thread "main" java.lang.Exception: Exception
    at Test.main(Test.java:11)

My Approach(simplified):

1  public class Test
2  {
3   public static void main(String ... args) throws Throwable
4   {
5       Exception e = new Exception("Exception");
6       switch(5)
7       {
8           case 1: throw e;
9           case 2: throw e;
10          case 3: throw e;
11          case 4: throw e;
12          case 5: throw e;
13      }
14  }
15 }

output:

Exception in thread "main" java.lang.Exception: Exception
    at Test.main(Test.java:5)
  • 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-01T01:04:36+00:00Added an answer on June 1, 2026 at 1:04 am

    the line it says the exception was thrown from is the line from where
    the exception was created, and not from where it was thrown. I’m just
    wondering why this is, and whether it is intended behavior for the jvm
    or not

    Yes. It is not only intended but documented. See the Javadoc for java.lang.Throwable: ‘A throwable contains a snapshot of the execution stack of its thread at the time it was created.’ If you don’t want that, you have the option of calling fillInStackTrace() just before throwing it.

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

Sidebar

Related Questions

I recently wrote some data access methods (plain old Java) that use immutable objects
I recently wrote a program that used a simple producer/consumer pattern. It initially had
I recently start studying JAVA, and I find this code: public class Dublicate{ public
I recently wrote a java crawler program that finds the video links in a
I recently wrote a theme function to add a class to my primary links
I recently found out about C# extension methods and wrote this one: /// <summary>
I recently wrote a java program transforming a SVG document to HTML/ Canvas :
I recently read that in newer computers Java's I/O performs better than NIO because
I recently wrote a Java playing card game and have all the game logic
I recently wrote a class for an assignment in which I had to store

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.