• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Exceptions(Handle and Declare)

 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI All.

Can any one explain the following statement with an example?

Although there are several different types of IOExceptions (EOFException, FileNotFoundException, and so on), by trapping IOExcepiton you can also catch instances of any subclass of IOException.


Thnak you in advance.

 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why don't you try it yourself? We can then tell you if you are on the right track. You will learn more that way.
 
Srikanth Iyer
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Barry.

import java.io.*;

class Check {
public static void main(String args[]){

try{
//code that might throw one or more exception of IOException type.
}
catch(IOException e1){ //Line1
//code to execute if a IOException is thrown.
}

try{
//code that might throw one or more exception of EOFException type.
}
//Can we catch the exception and handle it using the same code for catch block used at Line1.

}
}

Dose the example abouve justify the statement in my previous post.
As EOFException is a subclass of IOException.
Am i correct !!!
If yes what is the reason?

Thank you in advance.
 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
By trapping the more general exception you lose the information about what exactly was wrong. Different actions will be required when you get EOF versus file not found or others. EOF just means no data and file not there means exactly that the file is missing.

If you are writing a program where file not found is expected then IO exception will not tell you if this is what went wrong.

If you are writing a program that requires an empty file then IO excption would not tell you if the file ws empty or missing.
 
Joe Wolfe
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes. The more specific exceptions are caught up in IO Exception. This is why in the try and catch blocks you must put the most specific exceptions at the top of the list so they get caught before the more general ones.

You might later in the program check to see which exception thru it originally but it is easier to deal with it at the time it is thrown.
 
Srikanth Iyer
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Joe i got it. It is legal but the purpose of exception handling is not justified if used as aobve.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The statement above is saying that the following code works:
 
Can't .... do .... plaid .... So I did this tiny ad instead:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic