MATLAB: How to call a function on “dbstop if error”

dbstop

I have a large and complex function. I set "dbstop if error" at the start of the function. If the function fails for some reason it will hit dbstop if error.
At this point I wish to call a failed function which can email me the log file and other diagnostics, as well as letting me know that failure has occurred.
How can I do this?
For example, below is a function that will fail
function myFunction()
dbstop if error;
x = 1;
y = 2;
z = xy; % I will fail. After this failure, I would like the code to call a function called myFail.m
end

Best Answer

Hi,
why not using try/catch?
function myFunction()
try
x = 1;
y = 2;
z = xy; % I will fail. After this failure, I would like the code to call a function called myFail.m
catch err
myFail
end