Info

This question is closed. Reopen it to edit or answer.

Need a better solution than "try-catch" statement to find an intentional error in input arguments

1 view (last 30 days)
I know there are a lot of handy matlab functions that do what I must program, but I'm not allowed to use them. I need to be able to detect if the user enters the wrong data to the following. One of the test statements is:
y = day_diff(12,3,[3 4],5)
This obviously produces an error and my attempt to use "try-catch" in the code doesn't work. Does anyone have any ideas about how to catch this?
My code is:
function days= day_diff(M1,D1,M2,D2)
if nargin('day_diff') >4
days=-1;
return;
end
if D1<1 ||D2 <1
days = -1;
return;
end
try a = (M1 - floor(M1));
catch days =-1;
return;
end
try b = (M2 - floor(M2));
catch days = -1;
return;
end
try c = (D1 - floor(D1));
catch days =-1;
return;
end
try d = (D2 - floor(D2));
catch days = -1;
return;
end
if a ~=0 || b~=0
days =-1;
return;
end
if c ~=0 ||d~=0
days =-1;
return;
end
if isa(M1,'double') ~=1 || isa(D1,'double') ~=1 || isa(M2,'double') ~=1 || isa(M2,'double')~=1;
days = -1
return;
end
if M2 <1||M2 >12
days = -1;
return;
end
if M2 == 2 && D2 >28 || D2<1
days = -1;
return;
elseif (M2 == 4 || M2==6||M2==9||M2==11) && (D2>30 ||D2<1)
days = -1
return;
elseif D2 >31||D2<1
days = -1;
return;
end
if M1 <1||M1 >12
days = -1;
return;
end
if M1 == 2 && D1>28 || D1<1
days = -1;
return;
elseif M1 == 4 || M1==6||M1==9||M1==11 && D1>30 ||D1<1
days = -1
return;
elseif D1 >31||D1<1
days = -1;
return;
end
if M2<M1
M = M1;
M1 = M2;
M2 = M;
D = D1;
D1 = D2;
D2 = D;
end
if M1<M2
count=M1;
total=0;
if count<M2
if count==4 ||count==6||count==9||count==11
length=30;
elseif count==2
length = 28;
else
length=31;
end
total=total+length;
end
count = count +1;
if count <M2
if count ==4||count==6||count==9||count==11
length = 30;
elseif count==2
length = 28;
else
length = 31;
end
total = total +length;
end
count = count +1;
if count <M2
if count ==4||count==6||count==9||count==11
length = 30;
else
length = 31;
end
total = total + length;
end
count = count +1;
if count <M2
if count ==4||count==6||count==9||count==11
length = 30;
else
length = 31;
end
total = total + length;
end
count = count +1;
if count <M2
if count ==4||count==6||count==9||count==11
length = 30;
else
length = 31;
end
total = total + length;
end
count = count +1;
if count <M2
if count ==4||count==6||count==9||count==11
length = 30;
elseif count==2
length = 28;
else
length = 31;
end
total = total + length;
end
count = count +1;
if count <M2
if count ==4||count==6||count==9||count==11
length = 30;
else
length = 31;
end
total = total + length;
end
count = count +1;
if count <M2
if count ==4||count==6||count==9||count==11
length = 30;
elseif count==2
length = 28;
else
length = 31;
end
total = total + length;
end
count = count +1;
if count <M2
if count ==4||count==6||count==9||count==11
length = 30;
elseif count==2
length = 28;
else
length = 31;
end
total = total + length;
end
count = count +1;
if count <M2
if count ==4||count==6||count==9||count==11
length = 30;
elseif count==2
length = 28;
else
length = 31;
end
total = total + length;
end
count = count +1;
if count <M2
if count ==4||count==6||count==9||count==11
length = 30;
else
length = 31;
end
total = total + length;
end
count = count +1;
if count <M2
if count ==4||count==6||count==9||count==11
length = 30;
elseif count==2
length = 28;
else
length = 31;
end
total = total + length;
end
count = count +1;
if count <M2
if count ==4||count==6||count==9||count==11
length = 30;
elseif count==2
length = 28;
else
length = 31;
end
total = total + length;
end
count = count +1;
if count <M2
if count ==4||count==6||count==9||count==11
length = 30;
elseif count==2
length = 28;
else
length = 31;
end
total = total + length;
end
count = count +1;
if count <M2
if count ==4||count==6||count==9||count==11
length = 30;
elseif count==2
length = 28;
else
length = 31;
end
total = total + length;
end
count = count +1;
if count <M2
if count ==4||count==6||count==9||count==11
length = 30;
elseif count==2
length = 28;
else
length = 31;
end
total = total + length;
end
count = count +1;
if count <M2
if count ==4||count==6||count==9||count==11
length = 30;
elseif count==2
length = 28;
else
length = 31;
end
total = total + length;
end
end
if M1==M2 && D1==D2
days=0;
return;
end
if M1==M2 && D2>=D1
days= D2-D1;
end
if M1==M2 &&D1>=D2
days = D1-D2
end
if M1+1==M2
if M1 ==4||M1==6||M1==9||M1==11
length=30;
elseif M1==2
length = 28;
else
length=31;
end
days=length-D1+D2;
return;
end
if round(M2+1)==round(M1)
if M1 ==4||M1==6||M1==9||M1==11
length=30;
elseif M1==2
length = 28;
else
length=31;
end
days=length-D2+D1;
end
if round(M1+1)==round(M2)
if M1 ==4||M1==6||M1==9||M1==11
length=30;
elseif M1==2
length = 28;
else
length=31;
end
days=length-D1+D1;
return;
end
if M2>M1 && M1+1 ~=M2
days=total-D1+D2;
end
  4 Comments
KSSV
KSSV on 29 Nov 2016
I would prefer, running the code without try and catch; let the matlab stop and show me the error, so that I can change my code.
Walter Roberson
Walter Roberson on 29 Nov 2016
This is a school assignment where the programmer is responsible for detecting all possible errors and handling them smoothly. try/catch can be appropriate in some cases, especially if the programmer is forbidden from using some constructs that might allow the problems to be avoided.

Answers (0)

This question is closed.

Community Treasure Hunt

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

Start Hunting!