• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

mock exam Q

 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class sup
{ void method() throws Exception {} }

class test extends sup
{
public static void main( String [] args) // 1
{
sup s = new test();
s.method();
}
void method() // 2
{}
}
1) Add "throws Exception " clause at line 1
2) Add "throws Exception " clause at line 2
3) Use try catch block while calling s.method()
4) there is no compiler error
5) None of above

answer is 1) and 3)

I am not clear abt why we need to use throws exception clause on line 1
can anybody pls explain.


 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think your doubt will be clear if you refer to the javadoc tutorial from sun in the chapter "Specifying the Exceptions Thrown by a Method". It is very clear.

"If a method doesn't catch the checked exceptions that can occur within it, the method must specify that it can throw these exceptions. "
 
Ranch Hand
Posts: 262
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
By Sun:

Valid Java programming language code must honor the Catch or Specify Requirement



Take a look to this sun's document:
Lesson: Exceptions
 
Ranch Hand
Posts: 284
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Shaily,
As we know that if a class's method(here test) overrides any super class (sup here) method it must not throw new or broader exception then the base class and if the base class (SUP) is throwing any exception it must be defined (Using try catch block) in the sub-class (TEST) else it must declare that it throws exception(so Add "throws Exception ")
[ September 25, 2007: Message edited by: pranav bhatt ]
 
Ranch Hand
Posts: 424
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Here you are calling a method using a polymorphic object, so the compiler assumes you are calling the method defined in the super class(sup) which throws a checked exception, although the method that will get invoked at runtime is in the sub class(test) doesnt throw any exception, the compiler doesnt know that, and require you to handle or declare the exception.
 
Maybe he went home and went to bed. And took this tiny ad with him:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic