Just wrap your toggling in a try catch block is the only way to do it without a lot of code, eg
begin try set indentity_insert MyTable on end try begin catch print 'it already on dummy' end catch