gudii9
asked on
without try catch
Hi,
When i ran below program without try catch
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.BufferedRe aderExampl e.main(Buf feredReade rExample.j ava: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/perso nal/newfil e.txt
windows path in laptop
C:\Users\Desktop\gpfolder\ gpmy\perso nal\newfil e.txt
Please advise.
Any links resources ideas highly appreciated. Thanks in advance
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();
// }
// }
}
}
//}
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.BufferedRe
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/
windows path in laptop
C:\Users\Desktop\gpfolder\
Please advise.
Any links resources ideas highly appreciated. Thanks in advance
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
That was not the actual question I think
He asked two questions.
>New FileReader(C:\\Users\\Desk top\\gpfol der\\gpmy\ \personal\ \newfile.t xt)
Error! That should have been,
new FileReader("C:\\Users\\Des ktop\\gpfo lder\\gpmy \\personal \\newfile. txt")
Error! That should have been,
new FileReader("C:\\Users\\Des
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SO in this case why we need complicated try catches when stack trace is doing its workJust 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();
}
}
}
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)
ASKER
>>>>New FileReader(C:\\Users\\Desk top\\gpfol der\\gpmy\ \personal\ \newfile.t xt)
>>Error! That should have been,
>>new FileReader("C:\\Users\\Des ktop\\gpfo lder\\gpmy \\personal \\newfile. txt")
difference is 'n' should be small letter not capital in the 'new'
please advise
>>Error! That should have been,
>>new FileReader("C:\\Users\\Des
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.
Yes, and double quotes need to be around file path string.
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.FileNotFoundExcept ion: C:\Users\Desktop\gpfolder\ gpmy\perso nal\newfil e.txt (The system cannot find the path specified)
at java.io.FileInputStream.op en(Native Method)
at java.io.FileInputStream.<i nit>(FileI nputStream .java:120)
at java.io.FileInputStream.<i nit>(FileI nputStream .java:79)
at java.io.FileReader.<init>( FileReader .java:41)
at com.jpmc.ccs.cafad.dif.ala .job.Buffe redReaderE xample.mai n(Buffered ReaderExam ple.java:1 6)
I am not cler on above sentence. can you please elaborate.
now i got below exception from caller(here jvm)
Exception in thread "main" java.io.FileNotFoundExcept
at java.io.FileInputStream.op
at java.io.FileInputStream.<i
at java.io.FileInputStream.<i
at java.io.FileReader.<init>(
at com.jpmc.ccs.cafad.dif.ala
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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();
// }
// }
}
}
//}
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