Lesson 7: Exception Handling Part 2
Nothing is loaded from YouTube until you press play, and then without tracking cookies.
About this video
For a better learning experience, sign up and enroll in this free course at: daml.talentlms.com/plus/catalog/courses/150
Transcript
1,176 wordsCaptions as YouTube publishes them, usually machine-generated, so names such as Daml and Canton can be misheard.
- 0:00
previously you explored how to handle built-in errors such as precondition failed using try catch this lesson will focus on how to define a simple custom exception to throw and catch you define an exception with the exception keyword followed by the name of the exception the definition looks similar to templates and just as with templates the definition produces a record type of the given name as well as
- 0:31
instances to make that type throwable and catchable in addition to the record fields in the with block exceptions also need to define a message function in the word block to test throwing the custom exception remove the previously written try catch block and change the return type of the choice back to contract ID social network user then import the da action library to use the when function when takes two arguments the first
- 1:04
argument is a Boolean and the second argument is an action expression if the Boolean evaluates to true then the action expression gets evaluated otherwise nothing happens you will use this when function to check if new Alias is an empty string and if it is you will use the throw function to throw a change failed exception throw can be used in an action such as update and script an instance of an exception is created the same way a
- 1:36
record is created the change failed exception contains field name and details to specify which field failed to be modified if new Alias is not an empty string the when expression will have no effect allowing you to proceed with archiving self and creating a new contract using new Ileus you can run the test script where Alice attempts to exercise change Alias with an empty string this should confirm that
- 2:07
the custom exception change failed was thrown with field name and details you can also return a custom error in other words you can catch a generic error to throw a custom error instead first the return type of the choice needs to be either change failed or contract ID of social network user to indicate that it will either return a right Constructor containing a contract ID or a left Constructor containing a value of type change failed which is the
- 2:40
type of this custom exception in the try block you can specify what it is that you are attempting to do that might throw an exception in this case it's to Archive and create a new contract using the new Alias input the result is encapsulated in the right Constructor and returned using the pure expression if the new Alias is an empty text the create expression in the try block will fail and throw the precondition failed exception you can catch this exception in the
- 3:13
catch block and throw a custom error instead in the catch block there is no need to wrap the throw expression in the left Constructor like you did in the try block this is because throw can be used in an action such as update and script and it automatically matches its return type with the surrounding action to avoid potential type check errors in this case throw evaluates to an
- 3:40
update of either change failed or contract ID of social network user which aligns with the return type of the change Alias choice run the same test script where Alice attempts to exercise change Alias with an empty string to confirm that the custom exception change failed was thrown catching a generic error to return a custom error can be helpful if you aim to offer more informative error messages that are tailored to your application's context
- 4:11
this approach enables users to better comprehend the underlying reason for the error and take the necessary steps to address it since you confirmed that the custom exception works you can try to handle the exception again Implement a try catch block except this time throw a custom exception within the try block as before set the choice return type to either text or contract ID in the trido block first check if the new Alias is an empty string if it is
- 4:46
we'll throw the change failed exception with the field name Alias and details cannot be an empty string if the new Alias is not an empty string archive the contract and create a new one with the new alias then wrap it in the right Constructor to return it with the pure function as a reminder all that pure function does is to encapsulate in other words list the argument in update finally in the catchphrase the type of
- 5:18
the exception and its arguments are specified the variable F refers to the field name and D refers to the details knowing that both F and D are of type text you can concatenate them with a space the text is wrapped in the left Constructor for pattern matching and returned as the pure expression run the test script to confirm that the left value was returned from the catchphrase leaving the original contract active as you did with the built-in exception
- 5:51
you can also write the catch block using a different syntax in the line containing e colon change failed the variable e represents an exception of type change failed the variable e can be printed and it's an entire error record you can also pass the exception to the message function which prints the error message associated with the exception you can either use the default error message or access specific Fields within the exception record using dot notation such
- 6:24
as e dot field name and E dot details you can wrap the resulting text in the left Constructor to return it using the pure expression let's run the test script to confirm the return value encapsulated in the left Constructor under trace the first debug expression prints the exception record and the second debug expression prints the default error message associated with the exception finally you can add an additional statement at the end to catch any
- 6:57
exception any exception is a wrapper for all exception types which could work as a safety net in case you are unsure what other exceptions could potentially be thrown in this case if an error doesn't get handled by the first statement which is defined specifically for change failed exception it'll be handled by the statement defined for any exception this lesson explored the process of defining a custom exception an exception is structured like a template generating
- 7:30
a record type and instances that enable throwing and catching however exceptions have an additional requirement of defining a message function within the where block like built-in exceptions a custom exception can be manually thrown using the throw function within the try block and subsequently handled within the catch block when uncertain about the specific exception that might occur you can define a statement in the catch block for the any exception type which serves
- 8:01
as a wrapper for all built-in exception types