Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

throwing an exception

 
Maria Garcia
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if a method in a superclass does NOT throw an exception, and if the subclass overrides the method, can the latter throw an exception ?
 
Anthony Villanueva
Ranch Hand
Posts: 1055
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No.
Suppose class A has method m() which throws exceptions X, Y and Z.
Suppose class B extends A and overrides m().
Then m() of B can only throw:
1. X, Y, Z (some or all)
2. subclasses of X, Y, Z (some or all)
3. none at all
but m() cannot throw exceptions W or U.
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
m() (in the subclass) may throw any RuntimeException or Error, though
For more information, please read:
JLS 11.2 Compile-Time Checking of Exceptions
[ July 24, 2002: Message edited by: Valentin Crettaz ]
 
Anthony Villanueva
Ranch Hand
Posts: 1055
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah I meant checked exceptions X, Y, Z, W or U. Hope it's clear now
 
Maria Garcia
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you for your help
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic