0

I have a condition like this

String str = null;

try{
  ...
  str = "condition2";
}catch (ApplicationException ae) {
  str = "condition3";
}catch (IllegalStateException ise) {
  str = "condition3";
}catch (Exception e) {
  str = "condition3";
}

if(str == null){
  str = "none";
}

Now I want to sum up all str = "condition3"; in one line. As finally block runs always so that will not fulfill my needs. What else can be done.

Muhammad Imran Tariq
  • 22,654
  • 47
  • 125
  • 190
  • 1
    what do you mean by "sum up"? – michel-slm May 29 '12 at 05:24
  • 2
    I'm not sure what benefit you're looking for... if you want the error string to be the same for all 3 exceptions, then use the below answers. If not, and there's more code in each of the exception blocks we're not seeing, then I don't see that repeating one line is all that bad. – billjamesdev May 29 '12 at 05:27
  • what does it mean "all str="condition3"? if you want to sum up all str, it will be consist from cond2 and one from three exceptions. – Dmitry Zagorulkin May 29 '12 at 05:29
  • @ Bill James: After all this. Only "I don't see that repeating one line is all that bad" idea will work for me. – Muhammad Imran Tariq May 29 '12 at 05:37
  • Dont you think that there should be a block like catchFinally.... – Muhammad Imran Tariq May 29 '12 at 05:52
  • Suppose you have a `catchFinally`, show us how it would work. – weston May 29 '12 at 08:11
  • 1
    I suggest that this 'requirement' is evidence of a much bigger problem. More probably you should be letting the exception propogate, rather than assigning special values based on whether or not you got an exception. And how exactly is `str` ever going to be null? – user207421 May 29 '12 at 10:16

6 Answers6

6

Beginning in Java 7, you can catch multiple exception types in a single catch block. The code looks something like this:

String str = null;

try {
    ...
    str = "condition2";
} catch (ApplicationException|IllegalStateException|Exception ex) {
    str = "condition3";
}

BTW: The code you've posted, as well as my Java 7 code could all be collapsed into simply catch (Exception e), because Exception is a superclass of both ApplicationException and IllegalStateException.

Asaph
  • 159,146
  • 25
  • 197
  • 199
  • 3
    But isn't that pointless since the first two extend `Exception`? He should just catch `Exception` as long as he's doing that anyway. – Paul Bellora May 29 '12 at 05:25
  • I'm guessing he's not showing all the code there, so grouping them like this probably won't work. – billjamesdev May 29 '12 at 05:25
  • @PaulBellora: Yes. But the same is true of the OP's equivalent Java 6 code. I'm just demonstrating the syntax. Nevertheless, I'll add a note about that. – Asaph May 29 '12 at 05:26
  • Catch (ApplicationException|IllegalStateException|Exception) : is that necessary to keep Exception there........ as it is super class, i mean Exception alone is sufficient – Maddy May 29 '12 at 05:29
  • They are subclasses but I want to print different message for each sub exception. – Muhammad Imran Tariq May 29 '12 at 05:30
  • 2
    If you want to print a different message for each exception type, you need to do the three different blocks you posted in your original question. – Louis Wasserman May 29 '12 at 05:50
2

You can use Java 7 exception handling syntax. Java 7 supports multiple Exception handling in one catch block. Exp -

String str = null;

try{
  ...
  str = "condition2";
}catch (ApplicationException | IllegalStateException | Exception  ae) {
  str = "condition3";
}
Pramod Kumar
  • 7,914
  • 5
  • 28
  • 37
  • I dont want grouping exception as each exception will have a seperate message etc – Muhammad Imran Tariq May 29 '12 at 05:28
  • In this case you will have to follow traditional try catch block. try{ ... str = "condition2"; }catch (ApplicationException ae) { str = "condition3"; }catch (IllegalStateException ise) { str = "condition3"; }catch (Exception e) { str = "condition3"; } – Pramod Kumar May 29 '12 at 06:37
1
try{
  ...
  str = "condition2";
}catch (Exception ae) {
 str = "condition3";
}

As all others are subclasses of Exception. If you want to show different messages, then can try as follow

try{
   ...
   str = "condition2";
}catch(ApplicationException | IllegalStateException e){
if(e instanceof ApplicationException)
    //your specfic message
    else if(e instanceof IllegalStateException)
    //your specific message
    else
        //your specific message
    str = "condition3";
}
Chandra Sekhar
  • 18,914
  • 16
  • 84
  • 125
1

You must add "final" keyword if you are using Java 7 feature of catching multiple exception in single catch block

catch (final ApplicationException|IllegalStateException|Exception ex) {
Kumar Vivek Mitra
  • 33,294
  • 6
  • 48
  • 75
0

As you are doing same thing in ApplicationException and IllegalStateException catch blocks and in general exception Exception catch block, then you can remove ApplicationException and IllegalStateException blocks.

Pokuri
  • 3,072
  • 8
  • 31
  • 55
0

I'm going to go out on a limb here and provide this:

String str = null;

 try{
     ...
     str = "condition2";
 }catch (Throwable e) {
    str = "condition3";
 }
 finally {
     if(str == null){
         str = "none";
     }
 }

If this is not what you mean by "sum up" then please clarify.

Please read

http://www.tutorialspoint.com/java/java_exceptions.htm http://docs.oracle.com/javase/tutorial/essential/exceptions/

george_h
  • 1,562
  • 2
  • 19
  • 37