aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes remove the compiler error....... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "remove the compiler error......." Watch "remove the compiler error......." New topic
Author

remove the compiler error.......

Harvinder Singh
Ranch Hand

Joined: Feb 14, 2003
Posts: 90
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();
}
}


Hard work beats talent<br />when talent doesn't work hard.<p> - Tim Notke
Mahendra Deshpande
Greenhorn

Joined: Dec 22, 2000
Posts: 14
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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: remove the compiler error.......