none
Exception rippling RRS feed

  • Question

  • Hello All,

    Can you please provide me guidance on the best practices to implement exception handling in code.

    Basically, I want to know that is it beneficial to use try catch in caller function or the called function? Example

    func1()

    {

    try

    {

    func2

    }

    }

    func2

    {

    try

    {

    ...code..

    }

    }

    is it beneficial to have try catch in func2?

    Please advise 

    Friday, May 8, 2015 7:34 AM

Answers

  • Not sure I understand your question. If an exception is thrown in the code, it will automatically bubble up the chain of calling functions until it hits a suitable catch block. You do not need to wrap any code in try/catch blocks until you are at the point where you want to handle exceptions. At that point you can log the exception StackTrace for example, which would tell you where the exception was thrown etc.

    Friday, May 8, 2015 8:44 AM
  • Hello Harkirat IIIT,

    >>How does func1 exception details differs from func2 exception details?

    If your fun1 contains other code which processes your other business, it would catch these exceptions if they occurs, however, the try-catch block cannot catch them.

    If the fun1 contains only the a call to func2, in my opinion, these two try-catch are duplicated.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, May 11, 2015 1:47 AM
    Moderator

All replies

  • Not sure about best practice, but I would only put a catch block into func2 if that function actually needs to handle a specific exception (for example, to retry if an IO exception occurs, etc). It really depends on the situation and the purpose of the function. Otherwise I would allow exceptions to bubble up.

    As to where exactly you should handle exceptions in a general case, that's where you are likely to see some debate. But certainly at some point, I would say a 'higher layer' such as the GUI or view-model level, you should ensure exceptions are caught and logged (and possibly a message displayed).

    Friday, May 8, 2015 8:12 AM
  • How does func1 exception details differs from func2 exception details?
    Friday, May 8, 2015 8:24 AM
  • Not sure I understand your question. If an exception is thrown in the code, it will automatically bubble up the chain of calling functions until it hits a suitable catch block. You do not need to wrap any code in try/catch blocks until you are at the point where you want to handle exceptions. At that point you can log the exception StackTrace for example, which would tell you where the exception was thrown etc.

    Friday, May 8, 2015 8:44 AM
  • Hello Harkirat IIIT,

    >>How does func1 exception details differs from func2 exception details?

    If your fun1 contains other code which processes your other business, it would catch these exceptions if they occurs, however, the try-catch block cannot catch them.

    If the fun1 contains only the a call to func2, in my opinion, these two try-catch are duplicated.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, May 11, 2015 1:47 AM
    Moderator