Cody

Problem 44357. Back to basics: throwing errors / exceptions

Solution 1285920

Submitted on 12 Oct 2017 by David Gines
This solution is locked. To view this solution, you need to provide a solution of the same size or smaller.

Test Suite

Test Status Code Input and Output
1   Pass
a = 10; b = 5; %disp(a ./ b) q_correct = 2; q = realDivision(a, b); assert( isequal(q, q_correct) )

2   Pass
a = [100:2:300; 400:2:600]; b = 2; %disp(a ./ b) q_correct = [50:150; 200:300]; q = realDivision(a, b); assert( isequal(q, q_correct) )

3   Pass
a = 24; b = [1 2 3 4 6 8 12 24]; %disp(a ./ b) q_correct = [24 12 8 6 4 3 2 1]; q = realDivision(a, b); assert( isequal(q, q_correct) )

4   Pass
a = sqrt(-4); b = 2; %disp(a ./ b) e = []; try realDivision(a, b) catch err %disp(err); disp(err.stack); e = err; end; e_correct.identifier = 'realDivision:complexInput'; e_correct.message = 'The realDivision function only operates on real inputs.'; %assert( isequal(e, e_correct) ) assert( isequal(e.identifier, e_correct.identifier) ) assert( isequal(e.message, e_correct.message) )

5   Pass
a = 12 * randi(8, 10); b = randi(4, 10); rNum = randi(10); cNum = randi(10); cplx = 12 * randi(8) * sqrt(-1); if rand() < 0.5, a(rNum, cNum) = a(rNum, cNum) + cplx; else b(rNum, cNum) = b(rNum, cNum) - cplx; end; %disp(a ./ b) e = []; try realDivision(a, b) catch err %disp(err); disp(err.stack); e = err; end; e_correct.identifier = 'realDivision:complexInput'; e_correct.message = 'The realDivision function only operates on real inputs.'; %assert( isequal(e, e_correct) ) assert( isequal(e.identifier, e_correct.identifier) ) assert( isequal(e.message, e_correct.message) )

6   Pass
a = 'MATLAB'; b = 'coding'; %disp(a ./ b) e = []; try realDivision(a, b) catch err %disp(err); disp(err.stack); e = err; end; e_correct.identifier = 'realDivision:incompatibleInput'; e_correct.message = 'The realDivision function is not defined for character inputs.'; %assert( isequal(e, e_correct) ) assert( isequal(e.identifier, e_correct.identifier) ) assert( isequal(e.message, e_correct.message) )

7   Pass
a = 'MATLAB'; b = 'Cody'; e = []; try realDivision(a, b) catch err disp(err); disp(err.stack); e = err; end; e_correct.identifier = 'realDivision:incompatibleInput'; e_correct.message = 'The realDivision function is not defined for character inputs.'; %assert( isequal(e, e_correct) ) assert( isequal(e.identifier, e_correct.identifier) ) assert( isequal(e.message, e_correct.message) )

MException with properties: identifier: 'realDivision:incompatibleInput' message: 'The realDivision function is not defined for character inputs.' cause: {0×1 cell} stack: [51×1 struct] 51×1 struct array with fields: file name line

8   Pass
a = "Computing"; b = "Algorithm"; e_correct.identifier = 'MATLAB:UndefinedFunction'; e_correct.message = char("Undefined function 'rdivide' for input arguments of type 'string'."); e = []; try realDivision(a, b) catch err disp(err); disp(err.stack); e = err; end; %assert( isequal(e, e_correct) ) assert( isequal(e.identifier, e_correct.identifier) ) assert( isequal(e.message, e_correct.message) )

MException with properties: identifier: 'MATLAB:UndefinedFunction' message: 'Undefined function 'rdivide' for input arguments of type 'string'.' cause: {} stack: [51×1 struct] 51×1 struct array with fields: file name line

9   Pass
a = {1, 7, 3}; b = {4, 8, 9}; e_correct.identifier = 'MATLAB:UndefinedFunction'; e_correct.message = char("Undefined function 'rdivide' for input arguments of type 'cell'."); e = []; try realDivision(a, b) catch err disp(err); disp(err.stack); e = err; end; %assert( isequal(e, e_correct) ) assert( isequal(e.identifier, e_correct.identifier) ) assert( isequal(e.message, e_correct.message) )

MException with properties: identifier: 'MATLAB:UndefinedFunction' message: 'Undefined function 'rdivide' for input arguments of type 'cell'.' cause: {} stack: [51×1 struct] 51×1 struct array with fields: file name line

10   Pass
a.itemOne = 10; a.itemTwo = 20; b.itemOne = 5; b.itemTwo = 15; e_correct.identifier = 'MATLAB:UndefinedFunction'; e_correct.message = char("Undefined function 'rdivide' for input arguments of type 'struct'."); e = []; try realDivision(a, b) catch err disp(err); disp(err.stack); e = err; end; %assert( isequal(e, e_correct) ) assert( isequal(e.identifier, e_correct.identifier) ) assert( isequal(e.message, e_correct.message) )

MException with properties: identifier: 'MATLAB:UndefinedFunction' message: 'Undefined function 'rdivide' for input arguments of type 'struct'.' cause: {} stack: [51×1 struct] 51×1 struct array with fields: file name line

11   Pass
ratio = randi(11) + 1; b = randi(8, 10); a = ratio * b; rNum = randi(10); cNum = randi(10); b(rNum, cNum) = nan; %disp(a ./ b) q_correct = ratio * ones(10); q_correct(rNum, cNum) = NaN; q = realDivision(a, b); assert( isequal(q(~isnan(q)), q_correct(~isnan(q_correct))) ) assert( isequal(size(q), size(q_correct)) ) assert( isequal(isnan(q), isnan(q_correct)) )

12   Pass
ratio = randi(11) + 1; b = randi(8, 10); a = ratio * b; rNum = randi(10); cNum = randi(10); b(rNum, cNum) = 0; %disp(a ./ b) q_correct = ratio * ones(10); q_correct(rNum, cNum) = nan; q = realDivision(a, b); assert( isequal(q(~isnan(q)), q_correct(~isnan(q_correct))) ) assert( isequal(size(q), size(q_correct)) ) assert( isequal(isnan(q), isnan(q_correct)) )

13   Pass
ratio = randi(11) + 1; b = randi(8, 10); a = ratio * b; idxNum = randi(100, [1, 5]); b(idxNum) = complex( b(idxNum) ); %disp(a ./ b) q_correct = ratio * ones(10); q = realDivision(a, b); assert( isequal(q, q_correct) )

14   Pass
% The first error must generate an MException with the following properties: % identifier = 'realDivision:complexInput', message = 'The realDivision function only operates on real inputs.'. % % The second error must generate an MException with the following properties: % identifier = 'realDivision:incompatibleInput', message = 'The realDivision function is not defined for character inputs.'.

Suggested Problems

More from this Author31

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!