3

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?

Liam
  • 27,717
  • 28
  • 128
  • 190
Nil Pun
  • 17,035
  • 39
  • 172
  • 294

1 Answers1

4

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/

Community
  • 1
  • 1
Martin Davies
  • 4,436
  • 3
  • 19
  • 40