When it's necessary to check @@trancount > 0 in try catch block?

Sometimes I saw the following code snippet. When is the if @@trancount > 0 necessary with begin try? Both of them? Or it’s a safe way(best practice) to check it always in case it’s rollback before the check?

begin tran
begin try
  ... just several lines of sql ...
  if @@trancount > 0 commit tran
end try
begin catch
  if @@trancount > 0 rollback tran
end catch

  • Where to set a UTC datetime value in n-tier application: Presentation Layer, Domain, or Database?
  • Replacing alphabets to zero in any given string in SQL
  • sp_executesql causing my query to be very slow
  • Set IDENTITY_INSERT ON is not working
  • Will a SQL Server Job skip a scheduled run if it is already running?
  • how to query sql server database size
  • One Solution collect form web for “When it's necessary to check @@trancount > 0 in try catch block?”

    I can think of a few scenarios to consider when dealing with @@trancount:

    1. The current transaction was called from another stored procedure which had
      its own transaction
    2. The current transaction was called by some .NET code with its own
    3. The current transaction is the only transaction

    I believe Remus Rusanu’s Exception handling and nested transactions handles all these possibilities.

    MS SQL Server is a Microsoft SQL Database product, include sql server standard, sql server management studio, sql server express and so on.