Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt related to exceptions

 
Rohit Raghunath
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
package SelfTest;





In the above code doStuff() is overridden and declares that it throws ArithmeticException.....whereas the method in base does not declare any exceptions!!!

how come then it is allowed to throw ArithmeticException....isn't ArithmeticException a checked exception??
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Click on that link that was auto added to your post and you'll get your answer.
 
gurpeet singh
Ranch Hand
Posts: 924
1
Fedora Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rohit Raghunath wrote:package SelfTest;

class MyException extends Exception { }

class Tire { void doStuff() { } }

public class ST_C5_16 extends Tire
{
public static void main(String[] args)
{
new ST_C5_16().doStuff();
}


void doStuff() throws ArithmeticException
{
System.out.println(7/0);
}
}




In the above code doStuff() is overridden and declares that it throws ArithmeticException.....whereas the method in base does not declare any exceptions!!!

how come then it is allowed to throw ArithmeticException....isn't ArithmeticException a checked exception??


Please use code tags.

Regarding your question first of all i would advice you to keep JLS and JAVADOC API in your arsenall. as you can see here http://docs.oracle.com/javase/6/docs/api/java/lang/ArithmeticException.html Arithmetic exception is not checked exception. it is unchecked exception .
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what you are talking about will not apply to an unchecked exceptions(runtime exceptions)
 
raju salla
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Guyz,
Related to your doubt about the exceptions, the overriden method can throw any exception that is subclass of the exception which is thrown by the base class method and also it might throw any RuntimeException. As ArithmeticException is subclass of RuntimeException the overriden method can throw arithmetic exception.

Thanks and Regards
Raju Salla
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic