Exceptions
Raising and catching exceptions, error information and when an exception is not caught.
6 questions
JuniorTheoryVery commonHow do you catch an exception in the 1C built-in language, and what runs then?
How do you catch an exception in the 1C built-in language, and what runs then?
Put the risky code between Попытка and Исключение; the handler goes between Исключение and КонецПопытки and runs only if an exception is raised. ИнформацияОбОшибке() in the handler describes the error, and ВызватьИсключение raises one.
Common mistakes
- ✗Expecting the lines after the failing one to run: control jumps straight to the handler
- ✗Thinking the handler runs every time: it runs only when an exception was raised
Follow-up questions
- →How do you pass a caught exception further with context added to its text?
- →What happens to an exception that no
Попыткаup the call stack catches?
JuniorTheoryCommonWhat does the program print?
What does the program print?
отмена
Open full question →Common mistakes
- ✗Expecting the lines after the failing call in the
Попыткаblock to run: the exception passes control toИсключениеat once - ✗Expecting the program to return into the
Попыткаblock after the handler: it goes on afterКонецПопытки
Follow-up questions
- →How does the handler get the text of the exception it caught?
- →What happens if
Списать(0)is called outside anyПопытка?
JuniorTheoryCommonWhat does the program print (lines separated by spaces)?
What does the program print (lines separated by spaces)?
лог Нет товара
Open full question →Common mistakes
- ✗Expecting a caught exception to be over, so a bare
ВызватьИсключениеdoes nothing: it passes the same exception up - ✗Expecting the inner handler to be skipped when an outer
Попыткаexists: the nearestПопыткаcatches the exception
Follow-up questions
- →Why do checks compare
ИнформацияОбОшибке().Описаниеrather than the full error text? - →When is it right to pass an exception on after logging it?
MiddleTheoryCommonHow does ОписаниеОшибки() differ from ИнформацияОбОшибке() in an exception handler?
How does ОписаниеОшибки() differ from ИнформацияОбОшибке() in an exception handler?
ОписаниеОшибки() returns the error as a string. ИнформацияОбОшибке() returns an object with fields such as Описание, НомерСтроки and Причина; pass it to ПодробноеПредставлениеОшибки for the log, КраткоеПредставлениеОшибки for the user.
Common mistakes
- ✗Expecting
ОписаниеОшибки()to give the line number and cause as separate fields: it returns only a string - ✗Showing the detailed presentation to the user: it is meant for the log, and the user gets
КраткоеПредставлениеОшибки
Follow-up questions
- →Where should the detailed presentation of an error be written?
- →How do you add context when passing an exception on to the caller?
MiddleTheoryCommonWhen should code not catch an exception at all, by the 1C development standards?
When should code not catch an exception at all, by the 1C development standards?
When the code cannot handle the error: let the exception reach the caller, which rolls back the transaction or shows the message. Catching to hide an error, or to log it and go on, leaves wrong data; Попытка wraps only code that reacts to it.
Common mistakes
- ✗Wrapping every call in
Попыткаjust in case: a handler belongs only where the code reacts to the error - ✗Logging an error and carrying on: the caller then works with half-done results
- ✗Leaving the handler empty: a swallowed exception hides the cause of a failure
Follow-up questions
- →How do you add context to an exception before passing it on?
- →Where is a small
Попыткаjustified, for example around a call to an external service?
SeniorTheoryOccasionalWhy can't you keep working with the database after an error inside a transaction?
Why can't you keep working with the database after an error inside a transaction?
When a database operation fails in a transaction, the platform marks the whole transaction as failed: it can no longer be committed, and further reads and writes in it fail. So the handler calls ОтменитьТранзакцию() and raises the exception again.
Common mistakes
- ✗Catching an error inside a transaction and continuing to write: the transaction is already marked as failed
- ✗Swallowing the exception after
ОтменитьТранзакцию(): the caller must learn that the operation failed - ✗Expecting only the failed write to be rolled back: the whole transaction is lost
Follow-up questions
- →What does the standard pattern of
НачатьТранзакциюwithПопыткаlook like? - →What happens to the outer transaction when a nested one is cancelled?