Cody

Problem 44357. Back to basics: throwing errors / exceptions

Throwing and handling errors (or exceptions) is an important part of practical programming.

Here your task is to provide an alternative to the built in elementwise division operator ./ (equivalent to the rdivide function) with the following differences:

  1. throws an error if any of the input elements have a non-zero imaginary component (and are therefore 'complex numbers' that are incapable of being correctly represented as real numbers);
  2. throws an error if any of the input elements are character arrays;
  3. yields NaN wherever the divisor (i.e. the denominator) is equal to zero.

This concept is analogous to the built in MATLAB function "realsqrt".

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.'.

All other behaviour should be identical to the mrdivide function. Any exceptions that would have been thrown by mrdivide should also be thrown by your function — with the same contents of the identifier and message fields.

Note that the difference in MATLAB between errors and exceptions is somewhat fraught.

Solution Stats

90.0% Correct | 10.0% Incorrect
Last Solution submitted on Jun 13, 2019