aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Exception Handling Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Exception Handling" Watch "Exception Handling" New topic
Author

Exception Handling

mamidi venkat
Ranch Hand

Joined: Jul 03, 2007
Posts: 63
Hi All,

Here i have doudt in the below program iam getting the compile time exception The Exception is Excep.java:16: "unreported e
ared to be thrown"
t.myMethod1();

My Question is why iam getting the compile time exception.can any one body explain me clearly .whenever we are using the compile exception should be caught in try/catch block for both methods or using throws keyword .iam unable to get this conecept.

import java.io.*;
class Excep {
public int myMethod1() throws EOFException
{
return myMethod2();
}
public int myMethod2() throws EOFException
{
// Some code that actually throws the exception goes here
return 1;
}
krishnamoorthy kitcha
Ranch Hand

Joined: Feb 04, 2006
Posts: 96
Hi Everything should be correct

Just add one more close brace which covers end of the class.

Just add }

then check.
mamidi venkat
Ranch Hand

Joined: Jul 03, 2007
Posts: 63
Thank for the reply

but my Question is not that.
if i am compiling why i am getting the compile time exception
"Excep.java:5: unreported exception java.io.EOFException; must be caught or decla
red to be thrown
return myMethod2();"
^
why this exception is coming what is the reason for that.?


import java.io.*;
class Excep {
public int myMethod1()
{
return myMethod2();
}
public int myMethod2() throws EOFException
{
// Some code that actually throws the exception goes here
return 1;
}
}
Gitesh Ramchandani
Ranch Hand

Joined: Feb 28, 2007
Posts: 274
only declaring that it throws exception won't work. in this case you have to have "declare as well as catch".

myMethod2() throws exception which is not caught. So provide a try/catch.

gitesh
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Exception Handling
 
Similar Threads
Exception Handling
compilation error
Exception Exception is not compatible with throws clause in MyClass.myMethod()
when will the exception will be caught ???
Java endian and bit order and signed and unsigned and python and more confusion than I can deal with