• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

remove the compiler error.......

 
Harvinder Singh
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class ThrowsChecking{
static int Checking() throws Exception{
int i=24/12;
if(i==2){ {
throw new Exception("i value is 2");
}
return i;
}
public static void main(String args[]) {
ThrowsChecking.Checking();
}
}
 
Mahendra Deshpande
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You either need to throw the exception in the main method as
follows
class ThrowsChecking {
static int Checking() throws Exception {
int i=24/12;
if(i==2) {
throw new Exception("i value is 2");
}
return i;
}
public static void main(String args[])
throws Exception {
ThrowsChecking.Checking();
}
}
or use try-catch block in main method as follows.
class ThrowsChecking {
static int Checking() throws Exception {
int i=24/12;
if(i==2) {
throw new Exception("i value is 2");
}

return i;
}
public static void main(String args[]) {
try {
ThrowsChecking.Checking();
} catch ( Exception excp ) {
System.out.println("Exceptin caught " + excp );
}
}
}
This is on the principle of either declare the exception or catch it.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic