• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Overriding a method that throws Exception PRoblem

 
Ranch Hand
Posts: 634
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Edit: I was wrong
 
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Gari jain is Right..
 
Mohit G Gupta
Ranch Hand
Posts: 634
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i have changed the code
please help
 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 9050
21
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
so Exception and Throwable are somewhat CheckedExceptions

am i right Abimaran ??
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic