Sugata Bagchi
Sugata Bagchi

Reputation: 143

Java RuntimeException catching multiple catch blocks

I have the following code

`

package com.test.Custom;

public class StreamError 
  {

    public static void main(String[] args) 
    {
        try
        {
            String str = "Hello";
        if (!str.equals("Hello"))
            {
                throw new RuntimeException("None of the Directories Exists-Message failed");
            }
           int inf = ster(9);
            System.out.print(inf);
        }
            catch (RuntimeException stex)
              {
               //getTrace().addWarning("Failing the message");
                RuntimeException rme = new RuntimeException("None of the Directories Exists-Message failed", stex);
               throw rme;
              }
        
    }
    public  static int ster(int intg)
    {  try
       {
        if (intg >6)
        {
            throw new RuntimeException("None of the files Exists-Message failed");
        }
        else
            return intg;
        }
    catch (RuntimeException ste)
      {
       //getTrace().addWarning("Failing the message");
        RuntimeException pme = new RuntimeException("None of the files  Exists-Message failed", ste);
     throw pme;
      }
        
    }
}
`

When I execute this for integer >6, e.g 9 I can see it throws Runtime exceptions from both the catch blocks- even though the Directory part is correct , i.e String str = Hello.

Exception in thread "main" java.lang.RuntimeException: None of the Directories Exists-Message failed
    at com.test.Custom.StreamError.main(StreamError.java:21)
Caused by: java.lang.RuntimeException: None of the files  Exists-Message failed
    at com.test.Custom.StreamError.ster(StreamError.java:39)
    at com.test.Custom.StreamError.main(StreamError.java:11)
Caused by: java.lang.RuntimeException: None of the files Exists-Message failed
    at com.test.Custom.StreamError.ster(StreamError.java:31)
    ... 1 more

But, when I run this for wrong directory(say abcd) and correct integer (say 2) it returns only the runtime exception from directory catch block.

Exception in thread "main" java.lang.RuntimeException: None of the Directories Exists-Message failed
    at com.test.Custom.StreamError.main(StreamError.java:22)
Caused by: java.lang.RuntimeException: None of the Directories Exists-Message failed
    at com.test.Custom.StreamError.main(StreamError.java:13

) am I missing something very basic? Please help.

Thanks Sugata

Upvotes: 0

Views: 357

Answers (2)

It is because,you are calling the ster (9) even if string is "Hello" in the first try block. So, on ster(9) you have an exception but this exception breaks the first try statement too. So, it enters to the both catch statements (first and second). If you dont want it, you can call this int inf = ster(9); outside of try statements.

Upvotes: 1

Vladimir Stanciu
Vladimir Stanciu

Reputation: 1536

If you catch an exception just to throw it further, it makes no sense of catching it in first instance. That is the problem in your example, don't throw the exception in the catch block, just handle it(log a message or something).

Upvotes: 0

Related Questions