function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
ztrankztrank 

Invalid Conversion at runtime type MyException to String

I have a class that extracts the Type name from an object Instance
public static String extractTypeName(Object classInstance) {
        return String.valueOf(classInstance).split(':').get(0);
    }

In my test class I force a custom exception (declared in the test class) to be thrown, which I am expecting.
catch (exception e) {
            System.assert(e instanceof MyException, e.getMessage());
            System.assertEquals('clear exception', e.getMessage());
        }
This works when I run the test in the sandbox and the test succeeds. However when I run it in production it fails the first assertions and the message I get is "Invalid conversion from runtime type testClass.MyException to String"

Here is the exception declaration
private class MyException extends Exception {}

Any ideas?
R Z KhanR Z Khan
Hi,

It almsot sounds like you called extractTypeName with your exception as a parameter. The issue is caused because you were trying to cast MyException to a String somewhere in your code. 

On another note. You have a potential Null Pointer exception in your code return String.valueOf(classInstance).split(':').get(0);
You should check if classInstance is null. and maybe surround String.valueOf(classInstance) with try catch in case someone passes a wrong value