0

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

4
  • Don't throw exceptions that you immediately catch and handle. Just do the handling logic instead. Commented Aug 25, 2020 at 6:21
  • @AndyTurner : agree. Just only need to handle exception , not throw Commented Aug 25, 2020 at 6:26
  • 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? Commented Aug 25, 2020 at 6:45
  • 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 ? Commented Aug 25, 2020 at 6:56

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

you are correct fatma,writing that piece outside main try block worked. I think I need to create a custom exception class to catch that exception. as the exceptions I am trying to handle is more of a logic based and not follow the standard way of getting exception.
thanks for the direction, I moved the logic outside of try. and kepth the other portion within try.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.