| Author |
Exception handling and I/o
|
deepak carter
Ranch Hand
Joined: Feb 19, 2011
Posts: 136
|
|
Hi All,
Just to save time i am putting questions from two different topic in same post
In this code main has declared that below code code throw an exception i.e IOException, so who will catch this exception according to method stack.
Second Question....i want to append the data which i send using PrintWriter println method...in the file which is already containing some data..
Thanks in advance
[Edit - added code tags - see UseCodeTags for details]
|
 |
Manoj Kumar Jain
Ranch Hand
Joined: Aug 22, 2008
Posts: 153
|
|
Please use the code tags to save the time.
This is the responsibility of the function(who has called) to handle (Catch or throw it further) the exception from called function.
If you are calling a function/method that is throwing an exception you should catch it properly and if you don't want to catch it then you must throw it further saying that I am not bother about the Exception, then next function calling your function/method need to handle it.
If you are catching a exception it means you knows/telling what should do if a exception occur, If you don't catch it then it means you are throwing this away to next function.
So if you are throwing exception from the main method and you are not calling that main method from other class rather main method is called from JVM itself then your program will exit prematurely.
|
Do not wait to strike till the iron is hot; but make it hot by striking....
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 26720
|
|
If you get a copy of Thinking in Java™ by Bruce Eckel, you can read that an Exception not caught anywhere else propagates until it reaches the JVM, which prints a stack trace and terminates the thread in which the Exception occurred. Since you are in a single-threaded environment, your entire JVM will shut down. You can throw an Exception and still have JVM running like thisNow there’s a useful bit of code
|
 |
Winston Gutkowski
Bartender
Joined: Mar 17, 2011
Posts: 1610
|
|
deepak carter wrote:Second Question....i want to append the data which i send using PrintWriter println method...in the file which is already containing some data...
I suggest you look at FileOutputStream; specifically this constructor.
Winston
|
More computing sins are committed in the name of efficiency (without necessarily achieving it)
than for any other single reason...including blind stupidity. — W.A. Wulf
|
 |
Jeff Verdegan
Bartender
Joined: Jan 03, 2004
Posts: 3143
|
|
Manoj Kumar Jain wrote:
This is the responsibility of the function(who has called) to handle (Catch or throw it further) the exception from called function.
If you are calling a function/method that is throwing an exception you should catch it properly and if you don't want to catch it then you must throw it further saying that I am not bother about the Exception, then next function calling your function/method need to handle it.
And you should only catch it if you're going to actually do something about it, such as informing the user that his task failed, and stopping the rest of the process; or retrying; or continuing on with some avlid default value. Just logging it and continuing on as if everything is fine is not the right approach. Most exceptions should make it fairly high up the stack before they're finally stopped.
"Throw early, catch late."
|
 |
deepak carter
Ranch Hand
Joined: Feb 19, 2011
Posts: 136
|
|
So basically the conslusion is if the exception is caught no where it will go down to JVM and JVM will print stack trace and exit???
Am i right
?
|
 |
Jeff Verdegan
Bartender
Joined: Jan 03, 2004
Posts: 3143
|
|
deepak carter wrote:So basically the conslusion is if the exception is caught no where it will go down to JVM and JVM will print stack trace and exit???
Am i right
?
Correct.
|
 |
 |
|
|
subject: Exception handling and I/o
|
|
|