From: Alan B on
Walter Roberson <roberson(a)hushmail.com> wrote in message <hurgfk$mr7$2(a)canopus.cc.umanitoba.ca>...
> Alan B wrote:
>
> > You could use dbstack, loop over each m-file in the call stack, try to
> > parse the files to determine whether the current line is within any
> > try/catch-blocks, and return a logical vector containing the information
> > for the whole call stack.
>
> Heh, yes, until you hit an "eval" or "str2num" or similar call.
>
> > Or you could use the information from lasterror to try to guess whether
> > an error occurred in the immediately preceding try-block.
>
> That sounds to me like a tactic that should be able to detect if one is in a
> catch statement, but I can't think at the moment of any way that it could
> detect that one was in a try statement ?

Neither can I, but I figured a partial solution is better than nothing.

Also, OP's proposed function doesn't seem to distinguish between "inside catch statement" and "not inside catch statement OR try statement" - There are three states being represented by a boolean value. Using lasterror would allow you to assign 1 to "inside catch statement" instead of "inside try statement". In either case, there are two indistinguishable states remaining. Without knowing the intended usage, I couldn't say if this approach would be helpful at all.
From: Eric Salemi on
> Walter Roberson <roberson(a)hushmail.com> wrote in message
>
> ... Without knowing the intended usage, I couldn't say if this approach would be helpful at all.

Ok, I'll try to sum up why I came up with this idea.

At some point in the development of my project, I overloaded some matlab functions (error, warning, disp and rethrow) to performs extra processing and improve debugging.

One of the thing the overloaded error is doing is displaying in the command window extra debugging information. It would be nice if I could change the bevahior of this function whether I am ( in a try statement ) OR ( in a catch statement AND NOT in a try statement). This way I could keep a trace of all errors happening in cascaded try statements and only display them whenever an error within a catch statement but outside of any outter try statement occurs.

Example:

TRY1
TRY2
error <<< stored in stack (enclosed in TRY2)
CATCH
rethrow(last_error) <<< stored in stack (enclosed in TRY1)
END
CATCH
rethrow(last_error) <<< display current + 2 stacked errors
END

The solution of Alan B in message 4 looks feasible, I will try to go that way.
From: Steven Lord on

"Eric Salemi" <eric.salemi(a)septentrio.com> wrote in message
news:huta4m$1cv$1(a)fred.mathworks.com...
>> Walter Roberson <roberson(a)hushmail.com> wrote in message
>> ... Without knowing the intended usage, I couldn't say if this approach
>> would be helpful at all.
>
> Ok, I'll try to sum up why I came up with this idea.
>
> At some point in the development of my project, I overloaded some matlab
> functions (error, warning, disp and rethrow) to performs extra processing
> and improve debugging.

What additional debugging information are you asking these functions to
display? Or is the information specific to the specific application you're
writing?

> One of the thing the overloaded error is doing is displaying in the
> command window extra debugging information. It would be nice if I could
> change the bevahior of this function whether I am ( in a try statement )
> OR ( in a catch statement AND NOT in a try statement). This way I could
> keep a trace of all errors happening in cascaded try statements and only
> display them whenever an error within a catch statement but outside of any
> outter try statement occurs.
>
> Example:
>
> TRY1
> TRY2
> error <<< stored in stack (enclosed in TRY2)
> CATCH
> rethrow(last_error) <<< stored in stack (enclosed in TRY1)
> END
> CATCH
> rethrow(last_error) <<< display current + 2 stacked errors
> END

Are you aware of the MException object?

http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_prog/bq9l448-1.html

Specifying your CATCH block with an identifier and using the addCause method
sounds like it could do much of what you're trying to do with your overload.

sampleE = MException('', '');
try
try
error('CSSM:sampleID', 'Sample error message');
catch sampleE
rethrow(sampleE)
end
catch
try
error('CSSM:sampleID2', 'Sample error message 2');
catch sampleE2
sampleE2 = addCause(sampleE2, sampleE);
rethrow(sampleE2);
end
end

--
Steve Lord
slord(a)mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
To contact Technical Support use the Contact Us link on
http://www.mathworks.com


From: Andy on
"Eric Salemi" <eric.salemi(a)septentrio.com> wrote in message <hurdr8$9cq$1(a)fred.mathworks.com>...
> Hi all,
>
> I was wondering if there was a way to detect if the running code is within a try statement?
>
> something like:
>
> try
> bool = inside_try_catch(); <-- would return "true"
> catch
> bool = inside_try_catch(); <-- would return "false"
> end
>
> Thought about it for a while but could not find a solution...
>
> Regards,
> Eric.


tryflag = 0;

try
tryflag = 1;
% etc.
catch
tryflag = 0;
% etc.
end
From: Eric Salemi on
"Steven Lord" <slord(a)mathworks.com> wrote in message <hutdke$lp5$1(a)fred.mathworks.com>...
>
> > At some point in the development of my project, I overloaded some matlab
> > functions (error, warning, disp and rethrow) to performs extra processing
> > and improve debugging.
>
> What additional debugging information are you asking these functions to
> display? Or is the information specific to the specific application you're
> writing?

Yes, it is specific. The overloaded error.m tags each raised error with additional tags (which are dependent on the internal state of the application at the moment the error is raised). The tagged errors are stored in memory and immediately displayed on the screen and dumped to a file for further debugging.

The real issue I have --for the moment-- is that each raised error is treated the same way, whether it actually causes the code to stop execution or not. I want to be able to disable the display of those errors that are raised but don't cause the code to stop execution (Ex: the catch statement ignores the last error and let the code continue).

> > One of the thing the overloaded error is doing is displaying in the
> > command window extra debugging information. It would be nice if I could
> > change the bevahior of this function whether I am ( in a try statement )
> > OR ( in a catch statement AND NOT in a try statement). This way I could
> > keep a trace of all errors happening in cascaded try statements and only
> > display them whenever an error within a catch statement but outside of any
> > outter try statement occurs.
> >
> > Example:
> >
> > TRY1
> > TRY2
> > error <<< stored in stack (enclosed in TRY2)
> > CATCH
> > rethrow(last_error) <<< stored in stack (enclosed in TRY1)
> > END
> > CATCH
> > rethrow(last_error) <<< display current + 2 stacked errors
> > END

> Are you aware of the MException object?
>
> http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_prog/bq9l448-1.html

Yes, but I work with Matlab 7.1 and I think it is not available in this version.