Win a copy of Head First Android this week in the Android forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Can constructors throw Exceptions?

 
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I got it from http://www.angelfire.com/or/abhilash/Main.html

import java.io.IOException;


class Question72
{
public Question72() throws IOException
{
throw new IOException();
}
}

public abstract class Question73 extends Question72
{
public abstract void method();
}

An attempt to compile the above class definition

1. will cause a compiler error - non-abstract classes cannot be extended to abstract classes.
2. will cause a compiler error - a constructor must be provided which may or may not throw an IOException
3. will cause a compiler error - a constructor must be provided which must throw an IOException or one of its super types.
4. will not cause any compiler error. The class definition is perfectly legal.


Answer 3

Here I am bit confused about constructors throwing exceptions. I am not able to understand the exlanation given in the answer. Kindly help me out.

Thanks,
Geeta Vemula.
 
Sheriff
Posts: 9691
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A constructor CAN throw any exception. But if any subclass constructor calls a super class constructor which throws an exception, then the subclass constructor must either catch the exception or throw it. In the code given, since you don't provide any constructor in the class Question73, so a default constructor will be provided by the compiler. That default constructor will call super as it's first statement. So this will cause a compilation error. The code would look like this after compilation

 
geeta vemula
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But class Question72 is calling object class's constructor which is not throwing any exception then why dont it gives compiler error?
 
Ankit Garg
Sheriff
Posts: 9691
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A constructor can throw any exception even if it's super class constructor doesn't. But if a super class constructor throws any checked exception, then the subclass constructor must deal with it...
 
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Be very careful with constructors throwing exceptions. Here is the basic rule: If a superclass constructor throws a checked exception, then any subclass constructor that might end up calling that superclass constructor must throw the same exception, or a broader exception. The reason is that one of the subclass constructors will eventually call super(...) (and some or all other subclass constructors will do it either directly or via a this(...) invocation,) and there is no way that a subclass constructor can deal with the exception, since super(...) must be the first statement in the subclass constructor. In other words, you can't enclose it in a try/catch statement and deal with it.
[ January 01, 2009: Message edited by: Ruben Soto ]
 
geeta vemula
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That means if superclass constructor throws checked exception then subclass constructor either throws broader or same exception

But in case of methods, if superclass method throws checked exception then the overridding method in subclass may or may not throw same exception or narrower exception(i,e subclass of exception)

is it like that?
 
Ankit Garg
Sheriff
Posts: 9691
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's just opposite in the case of constructors and methods. A overriding method cannot throw broader checked exceptions than the overridden method in the super class. But in case of constructors, subclass constructor can throw broader checked exception but not narrower checked exception of the super class constructor. This behavior is opposite as constructors are not overridden...
 
geeta vemula
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks ankit and ruben for clearing my doubt.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic