• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Exception Handling

 
mamidi venkat
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic