File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Exception questions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Exception questions" Watch "Exception questions" New topic
Author

Exception questions

shaf maff
Ranch Hand

Joined: Sep 07, 2008
Posts: 180
Hi Guys

When an exception is thrown does the code refuse to finish off other tasks ? Or does it just log the error and continue onto the next piece ? And if an exception is thrown, the first thing you do is print/log that exception, but what methods do YOU as a programmer use to inform the user something went wrong ?
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11154
    
  16

I'm not sure what you mean by 'refuse to finish of other tasks'. The code does exactly what it is written to do. It's a well defined control structure. When an error is thrown, the first catch block that can handle that exception runs. What happens from there depends on what you write. You can pass control off to somewhere else, you can continue with the next iteration of a loop, you can ignore it and go on... it all depends on what your specific requirements are.

Next, i'd say you don't always WANT to inform the user. it would depend on what the error is, who the user is, and how severe it is. sometimes, simply writing an entry to a log file is enough. Other times, you may have to kill the whole process...


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
shaf maff
Ranch Hand

Joined: Sep 07, 2008
Posts: 180
Thanks for the reply. Ok, what if an error is thrown and in the catch block I write:


Now from what I know its going to create a new exception and basically you lose valuble information about the error. But what if I did:


Will, the "e" in the new exception carry on the information about the error or will all the error info still be lost ?
sree visu
Ranch Hand

Joined: Jul 05, 2007
Posts: 133


If this is written inside a method this exception will be thrown further to the bottom of the stack.Also
if error occours in statement 2 the statement 3 will not be executed and then the statement 4 will run as usual.
I hope this solves the issue



Regards<br />Sree Visu<br />SCJP 5.0, SCWCD 5.0, SCJD(Preparing B&S 2.2.2)
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11154
    
  16

shaf maff wrote:But what if I did:


Will, the "e" in the new exception carry on the information about the error or will all the error info still be lost ?


What happened when you tried it?
shaf maff
Ranch Hand

Joined: Sep 07, 2008
Posts: 180
sree: Thanks for the reply.

fred: I will try it in abit.

What Im trying to figure out here is what is the best method of handling exceptions/error outputs to users. So on a login page, if an error occurs the code should (1) log the error and (2) print out a simple message saying 'cannot connect to DB, please try again shortly' etc. Loggin the error is no problem, but what is the best way to output a message to the user telling him something went wrong ? Especially if they are filling in a registration form and they make errors on multiple fields ..
 
 
subject: Exception questions
 
Similar Threads
Capture javamail error to file
Hibernate or browser caching DB results?
Storing stacktrace of an exception to the database.
Multipart Upload - where does my file go?
Exception Handling