+ 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 ?
2 Antworten
+ 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.
+ 1
It probably depends essentially from the context ^^