Meaningless Drivel is fun!*
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
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,

Iam getting compile time exception why
Excep.java:5: unreported exception java.io.EOFException; must be caught or decla
red to be thrown
return myMethod2();
mean what.can anybody explain me

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;
}
}
Freddy Wong
Ranch Hand

Joined: Sep 11, 2006
Posts: 959

That means in your myMethod1(), you must either declare or catch the EOFException because myMethod2() declares the EOFException.


SCJP 5.0, SCWCD 1.4, SCBCD 1.3, SCDJWS 1.4
My Blog
mamidi venkat
Ranch Hand

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

what i understood is calling should be in the try/catch block
suppose iam using the throws keyword for the calling()it is showing the same exception.Exception must be caught or declare to be thrown mean what
Freddy Wong
Ranch Hand

Joined: Sep 11, 2006
Posts: 959


what i understood is calling should be in the try/catch block

Not necessarily.
You can do this:

or
mamidi venkat
Ranch Hand

Joined: Jul 03, 2007
Posts: 63
hi
Thanks a lot

public int myMethod1() throws EOFException
{ return myMethod2();
}

iam compiling the above code iam getting the exception
Excep.java:32: unreported exception java.io.EOFException; must be caught or decl
ared to be thrown
e.myMethod1();
Freddy Wong
Ranch Hand

Joined: Sep 11, 2006
Posts: 959

Like what I've already mentioned above. Whenever you call a method that declares a checked exception, you muse either declare it or catch it. For your information, declaring an exception means having "throws" clause in your method definition.

It seems that you're trying to call myMethod1(). In myMethod1() you declare EOFException, so whenever you call myMethod1(), you need to either catch the EOFException or declare it.

Hope this can clear your doubts.
mamidi venkat
Ranch Hand

Joined: Jul 03, 2007
Posts: 63
Hi

Thanks a lot but still my doudt is not cleared.

My Question is Why iam gettin the Exception after the declaration !

code is ->

import java.io.*;
class Excep {
int i=0;
/*public int myMethod1()
{
try{

i= myMethod2();
System.out.println(i);
}
catch(IOException e)
{
e.printStackTrace();
}
return i;
}*/

public int myMethod1() throws EOFException
{
return myMethod2();
}
public int myMethod2() throws EOFException
{
int a=10;int b=20;
int c=a+b;
return c;
}

public static void main(String args[])
{
Excep e=new Excep();
e.myMethod1();
}
}
Freddy Wong
Ranch Hand

Joined: Sep 11, 2006
Posts: 959

This is where your problem lies.

Did you declare the exception? No.
Did you catch the exception? Also no.
Why do you think you need to declare/catch the exception when calling myMethod1()? Because myMethod1() declares EOFException.

So what do you think that you need to do to fix it?
Declare it.

or catch it.
mamidi venkat
Ranch Hand

Joined: Jul 03, 2007
Posts: 63
Hi

Thanks a lot now iam cleared with the concept.
Freddy Wong
Ranch Hand

Joined: Sep 11, 2006
Posts: 959

That's great
mamidi venkat
Ranch Hand

Joined: Jul 03, 2007
Posts: 63
hi Freddy,

Iam preparing for the scjp exam.can you me tell me which book is good for this exam.give me your mail id for the technical help.if you dont mind
Freddy Wong
Ranch Hand

Joined: Sep 11, 2006
Posts: 959

Please check your private message.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Exception Handling
 
Similar Threads
Exception Handling
passing more than 1 parameter to method
clone method
when will the exception will be caught ???
Exception Exception is not compatible with throws clause in MyClass.myMethod()