wood burning stoves*
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


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
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.
 
wood burning stoves
 
subject: remove the compiler error.......
 
Similar Threads
printer not calling through this program..
problem with oops method::
pl explain
Mock Question Doubt...EQ+...Loops, Iterators and Exceptions
jqplus question: please help