There is a couple of problems with your code:
- First and foremost you need to understand the difference between checked exceptions and unchecked exceptions: Understanding checked vs unchecked exceptions in Java
Essentially you need to consider checked exceptions as part of your logic and you can only either rethrow them or handle them via catch
. The only other option if you need to avoid catch( CountDownExc )
is to add a throws CountDownExc
declaration to your method.
- These lines make little sense to me:
} catch (StackOverflowExc | NoClassDefFoundError e) {
throw new CountDownExc(50);
It looks like you intend on catching two exception types in the first catch
block, throw CountDownExc
in that first catch block, then handle it in the second catch
block. That's not gonna fly, because catch
blocks handle what is thrown in a try
block, not in a catch
block. Since CountDownExc
is a checked exception this is going to result in another compilation error, unless you either wrap your try
-catch
in another try
-catch
(bad practice) or add a throws
declaration.
Since both of your catch
es throw CountDownExc
the exception will have to be handled elsewhere anyway, so this may already be accounted for - I cannot tell, because the rest of your code is unknown.
Your immediate problem however comes from the fact, that nothing in your try
block is able to throw CountDownExc
and CountDownExc
is a checked exception, see first link.