File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Overriding a method that throws Exception PRoblem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Overriding a method that throws Exception PRoblem" Watch "Overriding a method that throws Exception PRoblem" New topic
Author

Overriding a method that throws Exception PRoblem

Mohit G Gupta
Ranch Hand

Joined: May 18, 2010
Posts: 634


getting compile-time error


if i put the following:
public static void main(String aasd[]) throws Exception

then,only Exception occurs


a.) Why???
b.)If i put RuntimeException instead of Exception in throws clause of foo method then,it not necessay to declare throws RuntimeException in main method throws
clause
WHY???


OCPJP 6.0 93%
OCPJWCD 5.0 98%
Eduardo Yañez Parareda
Ranch Hand

Joined: Oct 09, 2008
Posts: 92

Edit: I was wrong


http://serfj.sourceforge.net - Simplest Ever REST Framework for Java
Gari Jain
Ranch Hand

Joined: Jun 29, 2009
Posts: 100
The compiler error is not because of Exception handling. It is because at compile time compiler doesn't know about the object type, it sees that 'a' is of type 'A' and it cant find a function f() in A


OCPJP 6-100%; Preparing for GATE11
Rikesh Desai
Ranch Hand

Joined: Jun 02, 2010
Posts: 83
you need to either handle the exceptions that are getting thrown to main or write the main method ending with 'throws Exception'


OCPJP 95%
Eduardo Yañez Parareda
Ranch Hand

Joined: Oct 09, 2008
Posts: 92

Gari Jain wrote:The compiler error is not because of Exception handling. It is because at compile time compiler doesn't know about the object type, it sees that 'a' is of type 'A' and it cant find a function f() in A


Yes you're right.
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

As what the subject line says there is no overridden method in your code..


SCJP6.0,My blog Ranchers from Delhi
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

Gari jain is Right..
Mohit G Gupta
Ranch Hand

Joined: May 18, 2010
Posts: 634

i have changed the code
please help
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

mohitkumar gupta wrote:

if i put the following:
public static void main(String aasd[]) throws Exception

then,only Exception occurs

a.) Why???

If a method declare that it throws a Exception, then the user/caller should take the responsible to handle that exceptions. So, your foo() method throws exception, so you need to handle it or declare it as re-throwing it in the calling method, in your case, the main() method
mohitkumar gupta wrote:
b.)If i put RuntimeException instead of Exception in throws clause of foo method then,it not necessay to declare throws RuntimeException in main method throws
clause
WHY???

You don't need to handle the RuntimeExceptions!


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8829
    
    5
Hey Mohitkumar, and everyone else posting code,

You should try to format your code consistently (indents, use of curly braces, and so on), AND you should use JavaBeans naming standards for your classes, methods and variables. Otherwise the programmers who work with you might throw you out a window


Thanks,

Bert


Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
Mohit G Gupta
Ranch Hand

Joined: May 18, 2010
Posts: 634

so Exception and Throwable are somewhat CheckedExceptions

am i right Abimaran ??
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

mohitkumar gupta wrote:so Exception and Throwable are somewhat CheckedExceptions

Please go through this tutorial and this JLS
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

mohitkumar gupta wrote:i have changed the code
please help


PLease donot change you code like this..
You can start a new Topic for new code.

Changing a code like this will make the understanding difficult for someone who will read this post later
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Overriding a method that throws Exception PRoblem