Is it always a good practice to have your business rule code in try catch blocks ?

ujjwaljain
Kilo Explorer

Please can anyone let me know if it is always a good practice to wrap all code in try catch blocks in business rules in servicenow...

8 REPLIES 8

Amen, no more try catch please...

saprem_d
Giga Guru

Hi,



try Catch mechanism is suggested when you want to re execute some logic by using conditions like do while. This is majorly used for web services calls or in scenarios when you try to play with the session of user.



A good example is impersonating a user in "try", performing logging in "catch" and then re impersonate to self in "finally"


Jace Benson
Mega Sage

In client scripts it allows your scripts to fail and not make all your client scripts fail.


In business rules, they don't have the same dependencies on other scripts but the overhead of the a try/catch is something but how much can it be to suggest "not" using them? a.munoz & rajeshmushke



Insteresting related things Why isn't Try/Catch used more often in JavaScript? - Stack Overflow


seirra_p
Tera Contributor

This is old, but I was looking up links to add into best practices and came across this. 

I disagree if you are adding custom coding logic to the platform it is best to wrap in a try/catch. It's useful for when you are trouble shooting not only your own code but when something goes wrong as a way to track when users use the customizations in unexpected ways.