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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:25:23+00:00 2026-05-26T21:25:23+00:00

Is there a way to make Java Exceptions more informative? For example, take this

  • 0

Is there a way to make Java Exceptions more informative?

For example, take this code from the ClassCastException docs:

Object x = new Integer(0);
System.out.println((String)x);

Java will give me a ClassCastException with a message like “Can’t cast something of type Integer to String”. How can I make it say: “Can’t cast the Integer 0 to a String” instead?
And if I tried to cast a String “foo” to a Person, to make it say: “Can’t cast the String foo to a Person”? So with the value of the object I was trying to cast.

Can I somehow replace the standard ClassCastException by the more informative one, so I don’t have to introduce lots of try/catch-blocks? Subclassing is of course an option, but then I would have to introduce lots of try/catch-blocks.

The reason why I’m asking is actually because of another programming language which compiles to the JVM, Clojure.

In Clojure, beginners often make this mistake:

(def my-list ("foo" "bar"))

This results into an error message:

java.lang.String cannot be cast to clojure.lang.IFn

It would be very helpful for beginners to see something like:

java.lang.String "foo" cannot be cast to clojure.lang.IFn

so they would get the clue that they are trying to use a string a a function here.

It would be nice to be able to inject these new Exceptions, for a learning environment, without actually re-writing the Clojure Compiler. It could be solved at the REPL level by catching these types of exceptions though. Still I’m curious if this is possible with some nifty techniques.

  • 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-26T21:25:24+00:00Added an answer on May 26, 2026 at 9:25 pm

    This problem was discussed at the latest Clojure Conj and is generally accepted as something to work on in the compiler. There isn’t a lot you can do after the fact to improve the stack traces yet, but rest assured you are not the only one seeking to improve this.

    There is a good chance that the compiler will make the parse tree available to the build tools to enable people to create tools that will be able to interpret stack traces and print more meaningful messages, though these things will take time.

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

Sidebar

Related Questions

Is there a way to make this code work? LogonControl.java @Audit(AuditType.LOGON) public void login(String
Is there any way to make this work in Java? public static void change(List<?
Is there a way to make a java.awt.Font strike through?
Is there a way to make a class function unoverriddable? Something like Java's final
In Java (And in general) is there a way to make a class so
In Java, is there a way to truncate an array without having to make
Is there a way to make a global exception-handler in Java. I want to
Is there a way to make a TSQL variable constant?
Is there a way to make a Radio Button enabled/disabled (not checked/unchecked) via CSS?
Is there a way to make a popup window maximised as soon as it

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.