Link to home
Start Free TrialLog in
Avatar of gudii9
gudii9Flag for United States of America

asked on

without try catch

Hi,

When i ran below program without try catch

package com.mkyong.file;
 
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
 
public class BufferedReaderExample {
 
	public static void main(String[] args) {
 
		BufferedReader br = null;
 
	//	try {
 
			String sCurrentLine;
 
			br = new BufferedReader(new FileReader("C:/Users/Desktop/gpfolder/gpmy/personal/newfile.txt"));
 
			while ((sCurrentLine = br.readLine()) != null) {
				System.out.println(sCurrentLine);
			}
 
		//} catch (IOException e) {
	//		e.printStackTrace();
//		} finally {
//			try {
//				if (br != null)br.close();
//			} catch (IOException ex) {
//				ex.printStackTrace();
//			}
//		}
 
	}
}
//}

Open in new window


I see clear stack trace generated as below with line numbers

Exception in thread "main" java.lang.Error: Unresolved compilation problems:
      Unhandled exception type FileNotFoundException
      Unhandled exception type IOException

      at com.mkyong.file.BufferedReaderExample.main(BufferedReaderExample.java:17)


SO in this case why we need complicated try catches when stack trace is doing its work. Who prints stack trace compiler or jvm or program.

alos in IO programs why  always have to reverse forward slash to reverse slash of windlows path to make it work.

java program path
C:/Users/Desktop/gpfolder/gpmy/personal/newfile.txt
windows path in laptop
C:\Users\Desktop\gpfolder\gpmy\personal\newfile.txt


Please advise.


Any links resources ideas highly appreciated. Thanks in advance
SOLUTION
Avatar of Michel Plungjan
Michel Plungjan
Flag of Denmark image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
That was not the actual question I think
He asked two questions.
>New FileReader(C:\\Users\\Desktop\\gpfolder\\gpmy\\personal\\newfile.txt)  
Error! That should have been,
new FileReader("C:\\Users\\Desktop\\gpfolder\\gpmy\\personal\\newfile.txt")
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SO in this case why we need complicated try catches when stack trace is doing its work
Just checking, you do realise that this is NOT the same stack trace that would ever be printed out by the ex.printStackTrace(); on line 29 (had it not been commented out).

The stack trace that you got is from Java saying, "I can't run your program because I am unable to compile it due to a problem on line 17. That line calls a method that can throw IOExceptions and/or FileNotFoundExceptions and you MUST catch these exceptions". Note this... your program hasn't actually executed.

This is very different from what you get if you uncomment those lines. In that case, your program WILL be executed and then "if" the method call on line 17 encounters a problem, it will throw either IOException or FileNotFoundException that is caught by your catch statement and then line 29 prints the stack trace. As hinted by others, it is done in this way because there is any number of ways that these problems should be dealt with depending on what you are coding.


If you DO want your program to execute and you DON'T want to have to handle these potential problems yourself, this is the way that you would handle it...
package com.mkyong.file;
 
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
 
public class BufferedReaderExample {

    public static void main(String[] args) throws IOException {

        BufferedReader br = null;

        try {
            String sCurrentLine;

            br = new BufferedReader(new FileReader("C:/Users/Desktop/gpfolder/gpmy/personal/newfile.txt"));

            while ((sCurrentLine = br.readLine()) != null) {
                System.out.println(sCurrentLine);
            }

        } finally {
            if (br != null)br.close();
        }
    }
}

Open in new window

The important part to note is line 9, notice the throws IOException that I have added to the definition of your "main" method? What you are now saying is that some code in this method *may* throw an IOException and since we aren't handling here (with a catch statement), this exception should subsequently propagate to the caller of this "main" method. Since it is the JVM that is the caller of your main method, then it is the JVM that will catch the exception and print out a stack trace for you. (Note, I have left in the "finally" clause because it is still a good idea to manage your resources properly, even if you do allow the exception to propagate up. Although in this simple program you probably wouldn't worry about)
Avatar of gudii9

ASKER

>>>>New FileReader(C:\\Users\\Desktop\\gpfolder\\gpmy\\personal\\newfile.txt)  
>>Error! That should have been,
>>new FileReader("C:\\Users\\Desktop\\gpfolder\\gpmy\\personal\\newfile.txt")

difference is 'n' should be small letter not capital in the 'new'

please advise
>difference is 'n' should be small letter not capital in the 'new'.
Yes, and double quotes need to be around file path string.
Avatar of gudii9

ASKER

>> notice the throws IOException that I have added to the definition of your "main" >>method

now i got below exception from caller(here jvm)


Exception in thread "main" java.io.FileNotFoundException: C:\Users\Desktop\gpfolder\gpmy\personal\newfile.txt (The system cannot find the path specified)
      at java.io.FileInputStream.open(Native Method)
      at java.io.FileInputStream.<init>(FileInputStream.java:120)
      at java.io.FileInputStream.<init>(FileInputStream.java:79)
      at java.io.FileReader.<init>(FileReader.java:41)
      at com.jpmc.ccs.cafad.dif.ala.job.BufferedReaderExample.main(BufferedReaderExample.java:16)



SO in this case why we need complicated try catches when stack trace is doing its work
Just checking, you do realise that this is NOT the same stack trace that would ever be printed out by the ex.printStackTrace(); on line 29 (had it not been commented out).


I am not cler on above sentence. can you please elaborate.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gudii9

ASKER

The first exception was indicating compilation errors that you have ignored and said to proceed with launching the code. Yes, it is an exception that happens at run-time but it is indicating a compile-time problem (it is something particular to Eclipse that it still lets you run code that can't be fully compiled).
Actually when i try to run this time on other machine as below

package com.xyz;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class BufferedReaderExample {

	public static void main(String[] args) {

		BufferedReader br = null;

	//	try {

			String sCurrentLine;

			br = new BufferedReader(new FileReader("C:/Users/Desktop/gpfolder/gpmy/personal/newfile.txt"));

			while ((sCurrentLine = br.readLine()) != null) {
				System.out.println(sCurrentLine);
			}

		//} catch (IOException e) {
	//		e.printStackTrace();
//		} finally {
//			try {
//				if (br != null)br.close();
//			} catch (IOException ex) {
//				ex.printStackTrace();
//			}
//		}

	}
}
//}

Open in new window


I see this eclipse version showing errors

line 17 says -->resource leake, br never closed also unhandled filenotfound exception

then line 19 says-->unhandled IO exception