• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

Exception Handling

 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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;
}
}
 
Ranch Hand
Posts: 959
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That means in your myMethod1(), you must either declare or catch the EOFException because myMethod2() declares the EOFException.
 
mamidi venkat
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 959
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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


Not necessarily.
You can do this:

or
 
mamidi venkat
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 959
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 959
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

Thanks a lot now iam cleared with the concept.
 
Freddy Wong
Ranch Hand
Posts: 959
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's great
 
mamidi venkat
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 959
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please check your private message.
 
All of the following truths are shameless lies. But what about this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic