File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Exception Handling in Over-ridden method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Exception Handling in Over-ridden method" Watch "Exception Handling in Over-ridden method" New topic
Author

Exception Handling in Over-ridden method

Souvik Goswami
Greenhorn

Joined: Jan 17, 2012
Posts: 9



[Edit - added code tags - see UseCodeTags]
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4371
    
    8

Welcome to The Ranch!

What error were you expecting? Remember that ArithmeticException is a subclass of RuntimeException.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14114
    
  16

Welcome to the Ranch. Please check your private messages.

About your question: Do you mean: why don't I get a compiler error because I didn't specify ArithmeticException in the "throws" clause of the method? That is because ArithmeticException is an unchecked exception. See the page The Catch or Specify Requirement from Oracle's Java Tutorials, which explains this in more detail.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Souvik Goswami
Greenhorn

Joined: Jan 17, 2012
Posts: 9

Thanks, I understood that Unchecked Exceptions do not need to be thrown.
Now, is it possible to throw any Checked Exception in an over-ridden method, that is not mentioned to be thrown in the source method???
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19679
    
  18

Why don't you try it and see for yourself?


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Souvik Goswami wrote:Thanks, I understood that Unchecked Exceptions do not need to be thrown.


What you mean is that they don't need to be declared or caught. No exception ever needs to be thrown.



Now, is it possible to throw any Checked Exception in an over-ridden method, that is not mentioned to be thrown in the source method???


The overriding method can only throw the checked exceptions that its parent declares, and their descendants. So if the parent declares IOException, we can throw IOException, FileNotFoundException, etc., but we cannot throw SQLException.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4371
    
    8

Not checked exceptions, no (unless they're a subclass of something that is declared in the parent).

The reason is that it would violate the Liskov Substitution Principle. Consider this example:
If this was allowed, the compiler would allow the try/catch block, because it would treat the variable as a SuperClass object. It knows the only checked exceptions that can throw are IOExceptions, and these are handled. But obj is actually a SubClass object, and so line 12 might actually throw a SQLException. This would mean the compiler has allowed the possibility of a checked exception not being handled - which defeats the whole point of them. The sensible way to prevent this is to not allow the method to be overridden like this.
Souvik Goswami
Greenhorn

Joined: Jan 17, 2012
Posts: 9

Thanks every one.......
Rob Spoor:I was trying it out but faced some difficulties with constructor call like new XYZException(). But anyways, problem solved. Thank You all again.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Exception Handling in Over-ridden method