From ce66178d75acb25191e8191feb0d13e35d27c85b Mon Sep 17 00:00:00 2001 From: Enrico Lumetti Date: Fri, 30 Apr 2021 00:42:25 +0200 Subject: [PATCH] Exceptions --- string_operations.m | 3 +++ try_catch.m | 11 +++++++++++ 2 files changed, 14 insertions(+) diff --git a/string_operations.m b/string_operations.m index f810f13..dbd3faa 100644 --- a/string_operations.m +++ b/string_operations.m @@ -9,6 +9,9 @@ char_array = 'abc'; assert(all(char_array == ['a' 'b' 'c'])); assert(all(size(char_array) == [1 3])); +% single quotes are escaped by doubling +escaped = 'this is a ''char array'''; % this is a 'char array' + char_matrix = ['abc'; 'def']; assert(numel(char_matrix) == 6); diff --git a/try_catch.m b/try_catch.m index e69de29..8ed34ab 100644 --- a/try_catch.m +++ b/try_catch.m @@ -0,0 +1,11 @@ +clear +% matlab exceptions are represented by MException +try + non_existing + 3; +catch e + % identifier is the type of the exception + assert(isequal(e.identifier, 'MATLAB:UndefinedFunction')); + assert(isequal(e.message, 'Unrecognized function or variable ''non_existing''.')); +end + +% throw is used to throw a MException \ No newline at end of file