+ 1

Exception Handling and Functios Call

If I have a function that returns any value when it is called, and if an exception may occur in its operation, the exception handling must be done within the function or in function call ? What is the better practice ?

1st Feb 2017, 11:58 AM
Reginaldo Junior
Reginaldo Junior - avatar
2 odpowiedzi
+ 4
I say exception handling must be done INSIDE the function. Because in the process of the function itself, exceptions may occur. BEFORE the return statement. So if you wrap the function call, inside a try Catch block, when an exception occurs in the function process, it will never reach the return so the try catch won't be executed. It will just stuck inside the function and crash. This is why I'd always handle exceptions inside functions themselves.
1st Feb 2017, 12:32 PM
Alireza M
Alireza M - avatar
+ 1
It probably depends essentially from the context ^^
1st Feb 2017, 12:04 PM
visph
visph - avatar