The When Keyword
Sometimes you may need to catch an exception only when a particular condition exists. You can conditionally control the exception handling using the When keyword, which enables taking specific actions when a particular condition is evaluated as True. Continuing the example of the previous paragraph, the TestThrow class throws an ArgumentNullException in two different situations (although similar). The first one is if the string passed to the TestAccessFile method is empty; the second one is if the string passed to the method is a null value (Nothing). So it could be useful to decide what actions to take depending on what is the actual matter that caused the exception. According to this, we could rewrite the code shown in Listing ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access