Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Questions about method overloading and throwing exceptions...

 
Kai Wähner
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there,

two questions occured:

1) It is legal to create two methods:



But you cannot call the method with "test(1,2,3)" but only "test(new int[] {1,2,3})" because the first method call is ambigious.
So why is it allowed to create these two methods, if you cannot call the "int..." method? Or how can you call it?




2) If you add a throws declaration to a method for a checked exception, you have to throw one within the method, right?
But you can add "throws RuntimeException" and do not to have to throw one explicitly, right?

Why can you add "throws Exception" - which is not a RuntimeException - without throwing an exception within the method?


Best regards,
Kai

 
Ankit Garg
Sheriff
Posts: 9518
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kai Wähner wrote:2) If you add a throws declaration to a method for a checked exception, you have to throw one within the method, right?

No, a method can declare any checked exception in its throws clause even when it doesn't throw it...
 
Matthew Brown
Bartender
Posts: 4566
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kai Wähner wrote:
2) If you add a throws declaration to a method for a checked exception, you have to throw one within the method, right?
But you can add "throws RuntimeException" and do not to have to throw one explicitly, right?

This is the wrong way around. You don't have to throw a checked exception if you declare it. But you must declare it if you throw one. Whereas an unchecked exception never has to be declared whether you throw one or not.
 
Tom Reilly
Rancher
Posts: 618
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But you cannot call the method with "test(1,2,3)" but only "test(new int[] {1,2,3})" because the first method call is ambigious.
So why is it allowed to create these two methods, if you cannot call the "int..." method? Or how can you call it?

I posted a similar question (see http://www.coderanch.com/t/497670/java/java/var-args-overloading-compiler-error) but didn't get a response so I'm guessing here. Since var-args is a recent addition to the language, attempting to find a matching var-args method is one of the last things that the compiler checks. It first tries boxing and then gets confused because it doesn't know which method to call. Your example overloads Integer and int. My example overloads Integer and long and gets the same compiler error. Maybe in future versions the compiler can resolve this...
 
Sahil Kapoor
Ranch Hand
Posts: 316
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is allowed because of the following call....which distinguishes both....Compiler is smart enough.....!!!



Thanks !!!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic