• 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Exceptions

 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Consider the following code . Will it compile ? If yes , what is the output ?

import java . io . * ;

class Q031 {
{
try
{
throw new Exception ( ) ;
}
catch ( IOException e )
{
System . out . println ( e ) ;
}
}

Q031 ( ) throws Exception{ }

public static void main ( String args [ ] ) throws Exception {
System . out . println ( " GodSmack " );
}
};

Options :

a . The code compiles fine & throws java . lang . Exception on runtime
b . Compiles fine & prints " GodSmack " followed by the throwing of java . lang . Exception
c . Doesn't compile at all
d . Compiles & throws java . lang . Exception on runtime & prints " GodSmack " on runtime .

Answer: C.
Reason:
You can make the code compile by handling the Exception in the initializer .


Can anyone explain me the answer above..

Thanks in advance..

Rajani.
 
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello rajani,



In the above code, Exception is throw in the try block but it does not be catched in the catch block so the code cannot be compiled (Checked exception must be catched or reported).

To change the code in order to compile without error, you can put Exception or Throwable in the catch block. It is because Exception object is instanceof Exception and Throwable.



Thanks,
Jack
 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think here casting come into the picture.

You are thorwing an exception of type Exception which is a super class of IOException. As we know, We can not assign a super class object to a subclass reference variable.

It would work fine in the reverse direction. I mean to say you can catch an IOException with Exception reference variable (Ofcourse you can catch any exception with Exception reference variable).

Anyone, is there anything that i am missing here?
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I just compiled the code and when I ran, got the output "Godsmack".
I think the problem comes when we declare an instance of the class Q031..
Friends,can you verify this?
 
Baps Vakkalagadda
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are correct Siva. It gives an exception at runtime because IOException can not handle Exception of type Exception.

Here onething I did not understand. How the following snippet that is extracted from the above code is treated? When it is executed?

Can anyone explain me, please? That would help me for my certification exam which I am planning to take in the near future.
 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sivakumar,

you are correct. The code compiles fine and give the output GodSmack.

It is only when we try to instantiate the class we get the following output with exception:

GodSmack
Exception in thread "main" java.lang.Exception
at Q031.<init>(Q031.java:7)
at Q031.main(Q031.java:21)

Rads
 
Rajani Sudhakar
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all..

Thankyou soo much for your answers..
I got the conclusion..

The code complies fine but when the class is instantiated and run,
"Godsmack" is printed... and a run-time exception is thrown.

Regards,
rajani.
 
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The lessons we learnt here are
1. It is possible to throw a base exception and try to ctach its subclass- only the exceptio will not be caught at runtime.
2. The code itself compilese because there is a reverse inheritence heirachy.
3. This is related on some level to IOException e= (IOException) new Exception(); //which would cause a class cast exception at runtime
4. Since its okay to have Exception e= new IOException(); it is functional to throw IOException and catch Exception although bad practice
5. The code itself would have compiled properly had the catch clause been commented out since its existence was redundant. It was only due to the reverse inheritence heirachy that the compiler did not complain.
6. Non-Static initializers may throw checked exceptions if those exceptions are declared to be thrown in all the constructors.
[ May 28, 2004: Message edited by: Swamy Nathan ]
 
Get out of my mind! Look! A tiny ad!
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic