How to show the groovy stack from a try{}catch{}?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

How to show the groovy stack from a try{}catch{}?

David Budworth-3
Hi Folks,

I can't seem to find a page on the wiki explaining exception handling and was wondering if there's a means to show the groovy script stack when I do a try{} catch{} from within groovy.

If I let the exception bubble all the way up it shows fine, but if I catch it and printStackTrace I get the normal java style stacktrace.

Is there a special kind of catch syntax for getting a groovy wrapped exception?  Or some other magic to get the script stack?

Thanks,
-David

Reply | Threaded
Open this post in threaded view
|

Re: How to show the groovy stack from a try{}catch{}?

Jochen Theodorou
David Budworth schrieb:

> Hi Folks,
>
> I can't seem to find a page on the wiki explaining exception handling
> and was wondering if there's a means to show the groovy script stack
> when I do a try{} catch{} from within groovy.
>
> If I let the exception bubble all the way up it shows fine, but if I
> catch it and printStackTrace I get the normal java style stacktrace.
>
> Is there a special kind of catch syntax for getting a groovy wrapped
> exception?  Or some other magic to get the script stack?


I am a bit unsure what you mean... A script like this one:

try {
  throw new Exception("foo")
} catch (Exception foo) {
   foo.printStackTrace()
}

has a trace like this:

java.lang.Exception: foo
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at
sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
        at
sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:494)
        at
org.codehaus.groovy.runtime.MetaClassHelper.doConstructorInvoke(MetaClassHelper.java:506)
        at groovy.lang.MetaClassImpl.doConstructorInvokeAt(MetaClassImpl.java:1409)
        at groovy.lang.MetaClassImpl.invokeConstructorAt(MetaClassImpl.java:588)
        at
org.codehaus.groovy.runtime.Invoker.invokeConstructorAt(Invoker.java:191)
        at
org.codehaus.groovy.runtime.InvokerHelper.invokeConstructorAt(InvokerHelper.java:127)
        at
org.codehaus.groovy.runtime.ScriptBytecodeAdapter.invokeConstructorAt(ScriptBytecodeAdapter.java:118)
        at test.run(test.groovy:2)
[...]

and much more. test.groovy:2 is the position of the throw.

There is nothing like a groovy wrapped Exception. Every Groovy class or
script is mapped to a Java class with bytecode. Andy besides additional
method calls it is the same as a Java class. For example you can throw
an Exception in Java and catch it in Groovy, or the other way around.
The class of the Exception should not change. If it idoes, then it is a bug.

bye blackdrag

--
Jochen Theodorou
Groovy Tech Lead

---------------------------------------------------------------------
To unsubscribe from this list please visit:

    http://xircles.codehaus.org/manage_email

Reply | Threaded
Open this post in threaded view
|

Re: How to show the groovy stack from a try{}catch{}?

David Budworth-3
The stack I get does show the groovy class, but with no line number.

I was looking more for a stack like when you let an exception throw all the way out, where it's just a script stack, like below.

Just wondering if it's possible to get the simple script only stack.

*what I'm looking for*
[david@dlb-dev ~]$ groovy -e 'throw new Exception()'
Caught: java.lang.Exception
        at script_from_command_line.run(script_from_command_line:1)
        at script_from_command_line.main(script_from_command_line)

rather than:
*what I get if I catch it myself and log the stack*
[david@dlb-dev ~]$ groovy -e 'throw new Exception().printStackTrace()'
java.lang.Exception
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0 (Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java :27)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:494)
        at org.codehaus.groovy.runtime.MetaClassHelper.doConstructorInvoke(MetaClassHelper.java:506)
        at groovy.lang.MetaClassImpl.doConstructorInvokeAt (MetaClassImpl.java:1383)
        at groovy.lang.MetaClassImpl.invokeConstructorAt(MetaClassImpl.java:567)
        at org.codehaus.groovy.runtime.Invoker.invokeConstructorAt(Invoker.java:191)
        at org.codehaus.groovy.runtime.InvokerHelper.invokeConstructorAt (InvokerHelper.java:127)
        at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.invokeConstructorAt(ScriptBytecodeAdapter.java:123)
        at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.invokeNoArgumentsConstructorAt (ScriptBytecodeAdapter.java:138)
        at script_from_command_line.run(script_from_command_line:1)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
(clipped remailing 50ish lines)


On 7/11/06, Jochen Theodorou <[hidden email]> wrote:
David Budworth schrieb:

> Hi Folks,
>
> I can't seem to find a page on the wiki explaining exception handling
> and was wondering if there's a means to show the groovy script stack
> when I do a try{} catch{} from within groovy.
>
> If I let the exception bubble all the way up it shows fine, but if I
> catch it and printStackTrace I get the normal java style stacktrace.
>
> Is there a special kind of catch syntax for getting a groovy wrapped
> exception?  Or some other magic to get the script stack?


I am a bit unsure what you mean... A script like this one:

try {
  throw new Exception("foo")
} catch (Exception foo) {
   foo.printStackTrace()
}

has a trace like this:

java.lang.Exception: foo
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at
sun.reflect.NativeConstructorAccessorImpl.newInstance (NativeConstructorAccessorImpl.java:39)
        at
sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
        at java.lang.reflect.Constructor.newInstance(Constructor.java :494)
        at
org.codehaus.groovy.runtime.MetaClassHelper.doConstructorInvoke(MetaClassHelper.java:506)
        at groovy.lang.MetaClassImpl.doConstructorInvokeAt(MetaClassImpl.java:1409)
        at groovy.lang.MetaClassImpl.invokeConstructorAt (MetaClassImpl.java:588)
        at
org.codehaus.groovy.runtime.Invoker.invokeConstructorAt(Invoker.java:191)
        at
org.codehaus.groovy.runtime.InvokerHelper.invokeConstructorAt(InvokerHelper.java:127)
        at
org.codehaus.groovy.runtime.ScriptBytecodeAdapter.invokeConstructorAt(ScriptBytecodeAdapter.java:118)
        at test.run(test.groovy:2)
[...]

and much more. test.groovy:2 is the position of the throw.

There is nothing like a groovy wrapped Exception. Every Groovy class or
script is mapped to a Java class with bytecode. Andy besides additional
method calls it is the same as a Java class. For example you can throw
an Exception in Java and catch it in Groovy, or the other way around.
The class of the Exception should not change. If it idoes, then it is a bug.

bye blackdrag

--
Jochen Theodorou
Groovy Tech Lead

---------------------------------------------------------------------
To unsubscribe from this list please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: How to show the groovy stack from a try{}catch{}?

David Budworth-3
sorry, small correction.  I do get the script line number in the full java stack.  I just didn't see it since the script name actually shows up twice in the stack, first with a line number, second time without.


On 7/11/06, David Budworth <[hidden email]> wrote:
The stack I get does show the groovy class, but with no line number.

I was looking more for a stack like when you let an exception throw all the way out, where it's just a script stack, like below.

Just wondering if it's possible to get the simple script only stack.

*what I'm looking for*
[david@dlb-dev ~]$ groovy -e 'throw new Exception()'
Caught: java.lang.Exception
        at script_from_command_line.run(script_from_command_line:1)
        at script_from_command_line.main(script_from_command_line)

rather than:
*what I get if I catch it myself and log the stack*
[david@dlb-dev ~]$ groovy -e 'throw new Exception().printStackTrace()'
java.lang.Exception

        at sun.reflect.NativeConstructorAccessorImpl.newInstance0 (Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java :27)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:494)
        at org.codehaus.groovy.runtime.MetaClassHelper.doConstructorInvoke(MetaClassHelper.java:506)
        at groovy.lang.MetaClassImpl.doConstructorInvokeAt (MetaClassImpl.java:1383)
        at groovy.lang.MetaClassImpl.invokeConstructorAt(MetaClassImpl.java:567)

        at org.codehaus.groovy.runtime.Invoker.invokeConstructorAt(Invoker.java:191)
        at org.codehaus.groovy.runtime.InvokerHelper.invokeConstructorAt (InvokerHelper.java:127)
        at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.invokeConstructorAt(ScriptBytecodeAdapter.java:123)
        at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.invokeNoArgumentsConstructorAt (ScriptBytecodeAdapter.java:138)
        at script_from_command_line.run(script_from_command_line:1)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
(clipped remailing 50ish lines)



On 7/11/06, Jochen Theodorou <[hidden email] > wrote:
David Budworth schrieb:

> Hi Folks,
>
> I can't seem to find a page on the wiki explaining exception handling
> and was wondering if there's a means to show the groovy script stack
> when I do a try{} catch{} from within groovy.
>
> If I let the exception bubble all the way up it shows fine, but if I
> catch it and printStackTrace I get the normal java style stacktrace.
>
> Is there a special kind of catch syntax for getting a groovy wrapped
> exception?  Or some other magic to get the script stack?


I am a bit unsure what you mean... A script like this one:

try {
  throw new Exception("foo")
} catch (Exception foo) {
   foo.printStackTrace()
}

has a trace like this:

java.lang.Exception: foo
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at
sun.reflect.NativeConstructorAccessorImpl.newInstance (NativeConstructorAccessorImpl.java:39)
        at
sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
        at java.lang.reflect.Constructor.newInstance(Constructor.java :494)
        at
org.codehaus.groovy.runtime.MetaClassHelper.doConstructorInvoke(MetaClassHelper.java:506)
        at groovy.lang.MetaClassImpl.doConstructorInvokeAt(MetaClassImpl.java:1409)
        at groovy.lang.MetaClassImpl.invokeConstructorAt (MetaClassImpl.java:588)
        at
org.codehaus.groovy.runtime.Invoker.invokeConstructorAt(Invoker.java:191)
        at
org.codehaus.groovy.runtime.InvokerHelper.invokeConstructorAt(InvokerHelper.java:127)
        at
org.codehaus.groovy.runtime.ScriptBytecodeAdapter.invokeConstructorAt(ScriptBytecodeAdapter.java:118)
        at test.run(test.groovy:2)
[...]

and much more. test.groovy:2 is the position of the throw.

There is nothing like a groovy wrapped Exception. Every Groovy class or
script is mapped to a Java class with bytecode. Andy besides additional
method calls it is the same as a Java class. For example you can throw
an Exception in Java and catch it in Groovy, or the other way around.
The class of the Exception should not change. If it idoes, then it is a bug.

bye blackdrag

--
Jochen Theodorou
Groovy Tech Lead

---------------------------------------------------------------------
To unsubscribe from this list please visit:

    <a href="http://xircles.codehaus.org/manage_email" target="_blank" onclick="return top.js.OpenExtLink(window,event,this)"> http://xircles.codehaus.org/manage_email