Home > Enterprise >  How to find out the static and dynamic types of customized Exceptions
How to find out the static and dynamic types of customized Exceptions

Time:01-07

First to the task: I got to print out a certain String which includes the dynamic and the static type of my customized Exceptions. In a try and catch, I have to do the same thing with different ways of expressing the catch part.

I am a little confused about how to print out the static types and the dynamic types. Can I also just work with getClass()

so just for a more precise explaination, here is my code so far:

    String s1 = "1 : ";
    String s2 = "Exception : ";
    String s3 = "UpdateTimeBeforeLastUpdateException ";
    String s4 = "UpdateTimeInTheFutureException ";
    try {
        outsourced(ts, cal, 1);
    }
    catch(UpdateTimeBeforeLastUpdateException exc1) {
        System.out.println(s1   s2   s3   exc1);
    }
    catch(UpdateTimeInTheFutureException exc2) {
        System.out.println(s1   s2   s4   exc2);
    }
}

public void testCatch2(TimeStamp ts, Calendar cal, int test) throws Exception {
    String s1 = "2 : ";
    String s2 = "Exception : ";
    String s3 = "UpdateTimeBeforeLastUpdateException ";
    String s4 = "UpdateTimeInTheFutureException ";
    try {
        outsourced(ts, cal, 2);
    }
    catch(UpdateTimeBeforeLastUpdateException | UpdateTimeInTheFutureException exc) {
        System.out.println();
    }
}

private void outsourced(TimeStamp ts, Calendar cal, int n) throws Exception {
    switch (n) {
    case 1:
        ts.updateWithExc1(cal);
        break;
    case 2:
        ts.updateWithExc2(cal);
        break;
    case 3:
        ts.updateWithExc3(cal);
        break;
    case 4:
        ts.updateWithExc4(cal);
        break;
    case 5:
        ts.updateWithExc5(cal);
        break;
    } 

there are 3 more methods to come but my main problem is just the catch and outprint part

CodePudding user response:

If I got the question right, I assume that this may answer it:

try
{
    … // do something that causes an exception
}
catch( final OneException | TwoException e )
{
    if( e instanceof OneException e1 )
    {
        … // Handle OneException e1
    }
    else if( e instanceof TwoException e2 )
    {
        … // Handle TwoException e2
    }
    else throw new Error( "None of the expected exceptions caught" );
}

That instanceof syntax requires a later version of Java (14, I guess).

With the version 17 of Java (and the preview features enabled) it should be possible to use a switch statement, like this:

try
{
    … // do something that causes an exception
}
catch( final OneException | TwoException e )
{
    switch( e )
    {
        case OneException one -> … // Handle one
        case TwoException two -> … // Handle two
    }
}

CodePudding user response:

You can use multi-catch statments. And in the catch block, you can call getClass().getName() on the exception object. Now, you will have a fully qualified name of the exception class. If you just want the class name, not the fully qualified name, you can use String#split() and pass in a dot as a regular expression (\Q.\E, if you don't enclose dot with \Q and \E, it means that we are saying to match any character, not the dot) and get the last element from the returned array.

Here is how you do it: I am just rewriting your testCatch2()

public void testCatch2(TimeStamp ts, Calendar cal, int test) throws Exception {
    String s1 = "2 : ";
    String s2 = "Exception : ";
    try {
        outsourced(ts, cal, 2);
    } catch(UpdateTimeBeforeLastUpdateException | UpdateTimeInTheFutureException exc) {
        System.out.println(s1   s2   exc.getClass().getName());

        // If you don't want the fully qualified name, use the below statements.
        // String[] parts = exc.getClass().getName().split("\\Q.\\E");
        // System.out.println(s1   s2   parts[parts.length - 1]);
    }
}
  •  Tags:  
  • Related