exception problems

not sure how to catch this exception

Page 1 of 1

2 Replies - 2290 Views - Last Post: 15 August 2005 - 01:21 PM Rate Topic: -----

#1 chrome_bobcat  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 19-May 05

exception problems

Posted 19 May 2005 - 12:55 PM

Here's the basic structure of my program

import stuff;
-----------------------------------------------
public class MapImage extends JFrame {

public MapImage()
{
super( "Mapped Image" );

setSize( 400, 165 );
show();
}
-----------------------------------------------
public void paint( Graphics g )
{

}
-----------------------------------------------
public static void main( String args[] )
{
MapImage app = new MapImage();

app.addWindowListener(
new WindowAdapter() {
public void windowClosing( WindowEvent e )
{
System.exit( 0 );
}
}
);
}
}

The second method, public void paint, is the problem. I've left it blank because it's fairly long but I'll bring up the important parts here. First I open a file for input.

File inputFile = new File( "map.gz" );
FileInputStream in = new FileInputStream( inputFile );

Then I read the first three byte values of the file and use them to set the color.

g.setColor( new Color( c[0], c[1], c[2] ) );

I have a pixel drawn on the screen.

g.drawLine( x,y,x,y );

Then I read the next three bytes of the file to get the next color and print the next pixel untill the entire file has been read. I'm left with a graphical representation of the file where each pixel represents three bytes. Then

in.close();

But when I try to compile it I get these:

unreported exception java.io.FileNotFoundException
unreported exception java.io.IOException

Where do I need to type in the "throws IOException" statement in order to fix this? I've tried it at the end of all three methods but still get the same problem.

Is This A Good Question/Topic? 0
  • +

Replies To: exception problems

#2 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,507
  • Joined: 12-July 02

Re: exception problems

Posted 19 May 2005 - 07:31 PM

Before putting in the throw, you should try and solve why it's happening...the error you report would be thrown beacuse the file specified (or path) does not exist. Is the file there? Also, gz is a gzip file...is your file actually one with a gzip extension, or does that file contain the actual file you want to open?
Was This Post Helpful? 0
  • +
  • -

#3 1lacca  Icon User is offline

  • code.rascal
  • member icon

Reputation: 44
  • View blog
  • Posts: 3,822
  • Joined: 11-August 05

Re: exception problems

Posted 15 August 2005 - 01:21 PM

Just two more clues.

Throw must be written at the beginning of the function, like:
Return_type function_name(args) throws WhateverException{
...
}

But if you do this, you start a chainreaction, and you have to catch that exception somewhere.

So it is better to fix it where it happens. A hot fix could look like this:

public void paint( Graphics g )
{
try{
...
}catch(Exception e){
e.printStackTrace();
}
}

But this is for testing only, a catch all construct.
To make your code more sophisticated, try to catch every exception separately, and treat them individually. Like this:

File someFile = null;
try{
someFile = new File( filename );
}catch( java.io.FileNotFoundException fnfe){
some dialog that the file was not found;
return;
}

And something similar, for reading fro mthe file.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1