1

I have several functions where I am casting an object to an explicit type.

The problem is, when this object is created, the type may not match the type I am casting to. Somewhere along the line there are some conversions that take place. However, it is possible the code never reaches these conversions. The fact that it doesn't is not an issue.

So when the code reaches the point where the casting will occur, all I need to do is put the part into a try catch block. In fact I don't even need to handle the exception in any special way.

But I have a lot of these functions. I would like to know if there is some way to wrap lines of code with a try catch block without actually writing out the try catch block. If I can call some function some how that will do it automatically.

try 
{ 
    // cast something 
} 
catch( ClassCastException e ) 
{ 
}

Instead I want to call a method that will put the try catch block around // cast something. Is there any way to do something like this?

Note: I don't do anything when I catch the ClassCastException. It is okay if I can't cast the object correctly, but I need to catch the exception so the code execution won't jump to some other place.

1

4 Answers 4

3

You can write a method like this to do the casting for you while ignoring any ClassCastException.

public static <I, O> O cast(I input, Class<O> outClass) {
    try {
        return outClass.cast(input);
    } catch (ClassCastException e) {
        return null;
    }
}

You can use it like this:

Number n = new Integer(1);
Integer i = cast(n, Integer.class);

But you can also improve the code to avoid exceptions:

public static <I, O> O cast(I input, Class<O> outClass) {
    if(outClass.isAssignableFrom(input.getClass())) {
        return outClass.cast(input); 
    } else {
        return null;
    }
}
2
  • This works. Is it possible to cast an array of objects too? How can I pass the array in as an argument? For example, how would I change Integer.class to an array of Integers?
    – Mars
    Feb 18, 2016 at 5:54
  • Yes it is Integer[] i = new Integer[2]; Number[] n = cast(i, Number[].class);
    – MartinS
    Feb 18, 2016 at 5:56
0

Not really, because this doesn't make any sense. If there's nothing you need to do when you catch the exception, then don't call the operation at all and delete all the code after. If it doesn't matter whether the operation succeeds or fails then don't call it in the first place.

More seriously - ahem, that was serious - you can not catch the exception and let the caller deal with it.

1
  • I have other data that is stored that won't be returned unless I catch the exception. The part where I'm making a conversion is only part of the data class that I am eventually returning.
    – Mars
    Feb 19, 2016 at 4:13
0

Try to create a common method that does this casting for you

private MyTypeOfObject cast (Object obj) {

    try {

       // your casting code
       return newObj;

    } catch (ClassCastException ee) {
          // log and ignore
          return null; // ????
    }

}
4
  • what do you return in the "catch" block?
    – djechlin
    Feb 18, 2016 at 5:25
  • Based upon the OP question, I can not figure out what he does if he encounters a CCE. Maybe return null would be fine. Feb 18, 2016 at 5:27
  • Then the options are 1) null checks instead of try/catches or 2) throw an NPE instead of a CCE. I know I'm being harsh but it doesn't seem to me this approach does anything to resolve the problem.
    – djechlin
    Feb 18, 2016 at 5:28
  • Point taken. But the issue here as I see it, is how can I avoid duplicating common code. And the answer would be to a certain degree to use a common method. Feb 18, 2016 at 5:31
0

There's nothing that I know of to do this (other than what @Scary_Wombat said) but if I were in your shoes I would just write a simple code generator to handle these repetitive/boilerplate cases using some templating engine like Freemarker or something more advanced like Antlr. There are tutorials about the place on each.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.