From: Eric Salemi on
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.
From: Walter Roberson on
Eric Salemi wrote:

> 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

Tricky...

What if the try/catch was in another routine?

What if the catch rethrows the error?

What if the catch blocks are selective in the errors they catch, and thus
whether any particular error would be caught or not depends on exactly which
error it is?

Is "dbstop on error" not effectively at try/catch ? If so then the behavior
would depend upon whether you were debugging or not, which is seldom to be
desired.


Could you describe some advantages you see to having this facility?
From: Steven Lord on

"Eric Salemi" <eric.salemi(a)septentrio.com> wrote in message
news: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...

I don't believe there's any way to do this, but I'm curious -- if there
were, how would you use this information?

--
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: Alan B on
"Steven Lord" <slord(a)mathworks.com> wrote in message <hured5$g77$1(a)fred.mathworks.com>...
>
> "Eric Salemi" <eric.salemi(a)septentrio.com> wrote in message
> news: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...
>
> I don't believe there's any way to do this, but I'm curious -- if there
> were, how would you use this information?
>
> --
> 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
>

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.

Or you could use the information from lasterror to try to guess whether an error occurred in the immediately preceding try-block.

I'm also curious how this would be useful.
From: Walter Roberson on
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 ?