Is it always a good practice to have your business rule code in try catch blocks ?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-05-2017 09:41 AM
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...
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-05-2017 09:50 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-05-2017 10:07 AM
and also instance will become slower
Thanks,
Rajashekhar Mushke
Rising star : 2022 - 2024
Community Leader -2018
Connect me on LinkedIn : Rajashekhar Mushke
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-05-2017 10:12 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-05-2017 10:23 AM
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 async
programming. 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