This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of The Journey To Enterprise Agility and have Daryl Kulak & Hong Li on-line!
See this thread for details.
Win a copy of The Journey To Enterprise Agility this week in the Agile and Other Processes forum! And see the welcome thread for 20% off.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

Overriding a method that throws Exception PRoblem  RSS feed

 
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???
 
Ranch Hand
Posts: 93
IntelliJ IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Edit: I was wrong
 
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
 
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.
 
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
 
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!
 
author
Sheriff
Posts: 8977
17
  • 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
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!