• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

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,

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;
}
 
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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;
}
}
 
Ranch Hand
Posts: 274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Drove my Chevy to the levee but the levee was dry. A wrung this tiny ad and it was still dry.
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic