0
votes

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

2
Don't throw exceptions that you immediately catch and handle. Just do the handling logic instead. - Andy Turner
@AndyTurner : agree. Just only need to handle exception , not throw - Nilay Tiwari
My requirement is to stop the code execution if the directory is wrong or the number is greater than 6. how do I do that? I can write w/o a try-catch and can throw exception but, I have to use try catch for other exceptions to catch., can I just add such exceptions in the method signature and not use try-catch? - Sugata Bagchi
actually, the code block will not throw an exception in a standard way, but I have to check certain conditions - directory name/ integer value - based on that I have to stop the code execution and log a message. how this can be achieved ? - Sugata Bagchi

2 Answers

1
votes

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.

0
votes

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).