• 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

explaination about exception

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class Test9 extends A{
public static void main(String args[]) throws Exception{
Test9 t = new Test9();
}
}
class A{
A() throws Exception{
System.out.println("A Class");
}
}

this program gives compile time error
I do not understand the meaning of error
please explain and also tell me when a main method throws an
exception then where it is handled

krish Bajaj
 
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Default constructor of Test9 cannot handle Exception or throws the Exception.
So we should provide constructor for Test9 which could handle the Exception or throws the Exception.

Add this
Test9() throws Exception{}

Question for you : why we need this??
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's because the "class A" has a constuctor which throws an exception, so when some class extends this class, in this case the "class Test9", you have to deal with this exception in the constructor of the class which extends, so that when you call the Test9's constructor, it will call the super's constructor which throws an exception, that must be caught or thrown by the Test9's constructor.
And about the main method, when you throw an exception within a main method, there's nobody else who will be able to catch the exception, so the exception will not be caught.
 
krish bajaj
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hey vidya
please if u have any tutorial about this then please send
and if u can teach me then please help
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is from a bug report at Sun.

When a superclass constructor has a non-empty throws clause,
subclasses must define an explicit constructor with an
appropriate throws clause, as a default constructor has
no throws clause. (This is stated in JLS 2e 8.8.7, ruling
out the xxxxx alternative of copying the superclass
constructor's throws clause.

Currently, the compiler generates a default constructor
with an empty throws clause, and then generates an error
message. Unfortunately, the offending call, the implicit
call to the superclass constructor, does not appear in the
program text, so the message is confusing.


http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4278961

In the third edition of the Java Language Specification, the rules about default constructors are in 8.8.9.

http://java.sun.com/docs/books/jls/third_edition/html/classes.html#8.8.9
 
krish bajaj
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks Keith
now i understand
 
reply
    Bookmark Topic Watch Topic
  • New Topic