Thread View: comp.lang.java.programmer
2 messages
2 total messages
Started by Frederik Hansen
Tue, 10 Oct 2000 00:00
Why, oh, why??
Author: Frederik Hansen
Date: Tue, 10 Oct 2000 00:00
Date: Tue, 10 Oct 2000 00:00
49 lines
1699 bytes
1699 bytes
Hi
Why does this not work?? When printing the StackTrace it prints an
Exception, and not a RuntimeException as I hoped?
(This is a test setup, proberly not making any sense)
--- File : MethodOverload.java
public class MethodOverload
{
public static void main(String[] args)
{
try
{
throw new RuntimeException("e");
}
catch (Exception e)
{
try
{
throw TestObject.method(e);
}
catch (Exception e1)
{
e1.printStackTrace();
}
}
}
}
--- File : TestObject.java
public class TestObject
{
public static Exception method(Exception test)
{
return new Exception("1");
}
public static Exception method(RuntimeException test)
{
return new RuntimeException("Test");
}
}
Regards,
Frederik Hansen
Re: Why, oh, why??
Author: malissa
Date: Tue, 10 Oct 2000 00:00
Date: Tue, 10 Oct 2000 00:00
62 lines
2130 bytes
2130 bytes
It prints Exception because the variable "e" in your catch is of type
"Exception". If you change your variable's type to "RuntimeException",
you'll see the behavior you were expecting.
When passing variables as method parameters, the variable's type is what
determines which overloaded method is called.
Malissa
>
> Why does this not work?? When printing the StackTrace it prints an
> Exception, and not a RuntimeException as I hoped?
>
> (This is a test setup, proberly not making any sense)
>
> --- File : MethodOverload.java
>
> public class MethodOverload
> {
> public static void main(String[] args)
> {
> try
> {
> throw new RuntimeException("e");
> }
> catch (Exception e)
> {
> try
> {
> throw TestObject.method(e);
> }
> catch (Exception e1)
> {
> e1.printStackTrace();
> }
> }
> }
> }
>
> --- File : TestObject.java
>
> public class TestObject
> {
> public static Exception method(Exception test)
> {
> return new Exception("1");
> }
>
> public static Exception method(RuntimeException test)
> {
> return new RuntimeException("Test");
> }
> }
>
> Regards,
>
> Frederik Hansen
>
Sent via Deja.com http://www.deja.com/
Before you buy.
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads