This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Questions about method overloading and throwing exceptions... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Questions about method overloading and throwing exceptions..." Watch "Questions about method overloading and throwing exceptions..." New topic
Author

Questions about method overloading and throwing exceptions...

Kai Wähner
Ranch Hand

Joined: Sep 16, 2009
Posts: 76
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


My IT-blog about my experiences with: Java / JEE, IT Conferences and IT Certifications

Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9303
    
  17

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...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4398
    
    8

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

Joined: Jun 01, 2010
Posts: 618
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

Joined: Sep 12, 2009
Posts: 316
This is allowed because of the following call....which distinguishes both....Compiler is smart enough.....!!!



Thanks !!!


SCJP 6.0 96%

(Connecting the Dots ....)
 
GeeCON Prague 2014
 
subject: Questions about method overloading and throwing exceptions...