Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make exceptions more informative

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.

like image 617
Michiel Borkent Avatar asked Nov 15 '11 14:11

Michiel Borkent


3 Answers

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.

like image 68
Arthur Ulfeldt Avatar answered Oct 17 '22 11:10

Arthur Ulfeldt


Your question doesn't really make sense. Perhaps give a better example. The only thing that is remotely similar to what you are trying to do is intercepting exceptions with an Aspect-Oriented Programming framework (e.g. AspectJ). You can replace one exception with another, but probably won't give you the ability to actually access the object which failed to cast as in your example.

like image 28
MK. Avatar answered Oct 17 '22 12:10

MK.


This answer was typed before the question was edited and Clojure was mentioned.

Make a custom static method for casting to string like CastUtil.castString(). In this method you can check the type before attempting the cast and throw an informative exception that includes the value.

To make it easier to use this new method you can also use import static like this:

import static myutil.CastUtil.*;

Then in your code you can write castString(someObject)

like image 34
Sarel Botha Avatar answered Oct 17 '22 11:10

Sarel Botha