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

Rajesh Mushke
Mega Sage
Mega Sage

Hello Ujjwal Jain,



as per my knowledge : i won't say it's   a best practice, execution time of the business rule may increase...




Thanks,
Rajashekhar Mushke
Rising star : 2022 - 2024
Community Leader -2018
Connect me on LinkedIn : Rajashekhar Mushke

Rajesh Mushke
Mega Sage
Mega Sage

and also instance will become slower




Thanks,
Rajashekhar Mushke
Rising star : 2022 - 2024
Community Leader -2018
Connect me on LinkedIn : Rajashekhar Mushke

Aaron Munoz
Tera Guru

HiUjjwal Jain,



It would be more helpful to wrap in a try/catch block only part of a business rule that deals with data coming from ouside the instance. This is specially true if faulty data from the browser or a web service is likely to cause errors in your code. There are several OOB business rules with try/catch blocks and a lot of them handle parameters from the client.


Rajesh Mushke
Mega Sage
Mega Sage

The try-catch block is said to be expensive. However if critical performance is not an issue, using it is not necessarily a concern.


The penalty IMO is:


  • readability
  • inappropriate in many cases
  • ineffective when it comes to async programming

Readability: plumbing your code with plenty of try-catch is ugly and distracting


inappropriate: it's a bad idea to insert such block if your code is not subject to exception-crash. Insert it only if you expect a failure in your code. Take a look at the following topic: When to use try/catch blocks?


Async: the try-catch block is synchronous and is not effective when it comes to asyncprogramming. During an ajax request you handle both the error and success events in dedicated callbacks. No need for try-catch.




Thanks,
Rajashekhar Mushke
Rising star : 2022 - 2024
Community Leader -2018
Connect me on LinkedIn : Rajashekhar Mushke