Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Overriding a method that throws Exception PRoblem

 
Mohit G Gupta
Ranch Hand
Posts: 634
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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???
 
Eduardo Yañez Parareda
Ranch Hand
Posts: 93
IntelliJ IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Edit: I was wrong
 
Gari Jain
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Rikesh Desai
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you need to either handle the exceptions that are getting thrown to main or write the main method ending with 'throws Exception'
 
Eduardo Yañez Parareda
Ranch Hand
Posts: 93
IntelliJ IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As what the subject line says there is no overridden method in your code..
 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gari jain is Right..
 
Mohit G Gupta
Ranch Hand
Posts: 634
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have changed the code
please help
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Bert Bates
author
Sheriff
Posts: 8898
5
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Mohit G Gupta
Ranch Hand
Posts: 634
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so Exception and Throwable are somewhat CheckedExceptions

am i right Abimaran ??
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
mohitkumar gupta wrote:so Exception and Throwable are somewhat CheckedExceptions

Please go through this tutorial and this JLS
 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic