0

I have a Java Application code that has following custom Exception structure

  • SettlementException extends Exception
  • PolicyMissingException extends SettlementException
  • PolicyExpiredException extends SettlementException

I have some try/catch blocks in the code that try PolicyMissing and PolicyExpired and throw the same.

try
{
    if (Policy != null)
    {
        ...
    }
    else
    {
        throw new PolicyMissingException(“Policy missing”);
    }
}
catch(PolicyMissingException e)
{
  e.printstacktrace();
}

Is there any way I can throw SettlementException too besides PolicyMissing and PolicyExpired along with them?

msrd0
  • 7,816
  • 9
  • 47
  • 82
Bhavna
  • 117
  • 1
  • 2
  • 13
  • you can catch multiple exceptions - just add another catch block below your **catch(PolictMissingExc)** block – jbutler483 Sep 17 '14 at 13:04
  • http://stackoverflow.com/questions/3374966/what-is-the-java-equivalent-of-aggregateexception-from-net – David Pilkington Sep 17 '14 at 13:04
  • You mean throwing one of the two exceptions depending on the circumstances? Sure, you can. Just use if-else-if-else. Or do you mean throwing two exceptions *at the same time*? Sorry, you cannot do that. Maybe make a new exception class that combines the information you want to convey. – 5gon12eder Sep 17 '14 at 13:04
  • 2
    @jbutler483 You can also use a multiple catch block: **`catch(FirstException | SecondException e)`** – msrd0 Sep 17 '14 at 13:05
  • @msrd0 I was thinking more of if the OP wanted to catch/do something differently on each of the 'throws' – jbutler483 Sep 17 '14 at 13:06
  • 1
    I think that he is looking a Java version of a AggregateException in c# – David Pilkington Sep 17 '14 at 13:07
  • 1
    @jbutler483 I think its very unclear what the OP wants to know – msrd0 Sep 17 '14 at 13:10

3 Answers3

1

The below code should allow you to catch all three exceptions, and do something with each one. Also note, the order of the 'catches' is important too (see comment below this post)

try
{
    if(Policy!=null)
    {
        ...
    }
    else
    {
        throw new PolicyMissingExc(“Policy missing”);
    }
}
catch(PolicyMissingExc e)
{
  e.printstacktrace();
}
catch(PolicyExpired c)
{
  //catch c here
}
catch(SettlementException b)
{
  //catch b here
}
jbutler483
  • 24,074
  • 9
  • 92
  • 145
  • 4
    If you do this, just be sure that if one exception is derived from (extends) another one, put the most-derived type first or it will never be caught. – 5gon12eder Sep 17 '14 at 13:07
0
try
{
    if(Policy!=null)
    {
        ...
    }
    else
    {
        throw new PolicyMissingExc(“Policy missing”);
    }
}
catch(PolicyMissingExc e)
{
  e.printstacktrace();
}
catch(PolicyExpired e)
{
  e.printstacktrace();
}
catch(SettlementException e)
{
  e.printstacktrace();
}

You can catch multiple exceptions but remember catch block must be in order from subclass to superclass otherwise you will get Unreachable catch block compilation error.

Ankush
  • 675
  • 2
  • 13
  • 29
  • My policymissing and policyexpired exceptions are classes that extend settlementException. The way you have provided gives unreachable catch block error for settlementException. Saying only specific exceptions are thrown and handles by previous catch blocks – Bhavna Sep 17 '14 at 13:16
  • I mean you cannot catch SettlementException before its subclasses PolicyMissingExc or PolicyExpired. – Ankush Sep 17 '14 at 13:20
  • Yeah. Understood. That is why the code first catches policyExpired. But I want both to be caught, policyExpired and settlement. Which is not happening – Bhavna Sep 17 '14 at 13:22
0

Why not have just one exception class, SettlementException, and use it as a container (Set?) for all of the known problems?

Solomon Slow
  • 25,130
  • 5
  • 37
  • 57