Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

error thrown in a constructor..

 
arivu mathi
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
import java . io . * ;

class Q031 {
{


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


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

why the above code is showing error?
 
Gowher Naik
Ranch Hand
Posts: 643
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

check code above it is error free.
 
Awais Sheikh
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


just try this one. you are using extra brackets. Please check your code carefully.
 
arivu mathi
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
now the code compiles fine but it's showing runtime error..why?
 
David Kennedy
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think because you cant throw a general Exception and expect to catch
it with a narrower catch exception such as IOException
 
prarthana reddy
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because you are throwing an Exception but not handling it properly..
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic