Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why should I use Assert.IsNull in Sitecore?

I've seen a lot of code in Sitecore where Assert.IsNull is used before any logic;

e.g.

Database database = Factory.GetDatabase(itemUri.DatabaseName);
Assert.IsNotNull(database, itemUri.DatabaseName);
return database.GetItem(attribute);

Could someone help me understand why I would use this?

like image 526
Nil Pun Avatar asked Jan 28 '26 18:01

Nil Pun


1 Answers

This topic isn't really specific to Sitecore, even though in this case the assert methods are within the Sitecore library.

In general, assertions are used to ensure your code is correct during development, and exception handling makes sure your code copes in unpredictable circumstances.

Take a look at these SO questions for some very good explanations.

When to use an assertion and when to use an exception

When to use assert() and when to use try catch?

Here's an article specifically about the use of Sitecore assertions:

http://briancaos.wordpress.com/2012/01/20/sitecore-diagnostics-assert-statements/

like image 188
Martin Davies Avatar answered Feb 01 '26 01:02

Martin Davies



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!