Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Indicate non-fatal error without Exception

 
John Ryan
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone,
I have a problem relating to how i should handle non fatal errors. I have a static method to which i pass a List. The static method loops through the list, does some processing based on the contents of the list and then returns a new list object as a result. As i am processing an object in the list it is possible that an error may arise that may not allow me to process that object. However i do not wish to terminate the processing of the list (i.e. i want to continue looping through the list). I log the error and continue. However i need someway of indicating that an error has occurred and that when my application has finished executing that the user should check the log file.
I cannot throw an exception within the method as this would cause me to exit from the loop of processing all the objects in the list and i dont want this. I cannot return some type of error object as i already return the results of the processing. So how do i indicate that somthing is not right.
The only solution i have thought about is to pass some kind of collection object to the method to which i can add error strings when somthing goes wrong. When the method is finished executing the client of the method can check if there are any entries in the error Collection object it passed to the method. If there are then it will warn the application user.
Can anyone think of a better way of doing this??
Thanks,
John
[ August 01, 2002: Message edited by: John Ryan ]
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You might want to look into Loggers.
 
Stu Glassman
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John, I'd say that your solution sounds like the best idea. The reverse might also work. Have the method return the error codes, or perhaps just a boolean, and add the processed objects to a List (passed to it as a parameter).
-Stu
 
John Ryan
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Cindy Glass:
You might want to look into Loggers.


Hi Cindy. I am actually using log4j so its not the logging of the error that it is the problem. My problem revolves around how to indicate to the cleint of my method that an error has occurred without throwing an exception. Logging the error is one step but you need a way to indicate to the user that an error occurred and please check the error log for detaisl...
 
Ronald Francis
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well , I believe in " When in doubt, create another class" ;-)
I would wrap the results of what you're passing back from the method in a class that contains your original return value , plus suitable error codes/messages
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So if you logged an error, why don't you just force open the Log file for the user and SHOW it to him? Or just write a message to the GUI or console telling him to go look at it.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I understand the question - you can throw an exception from deep within whatever processing you're doing, and then catch it just inside the iterator loop:

[ August 01, 2002: Message edited by: Jim Yingst ]
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ronald Francis:
Well , I believe in " When in doubt, create another class" ;-)
I would wrap the results of what you're passing back from the method in a class that contains your original return value , plus suitable error codes/messages

Either that or use an object instead of a static method, so that you could do something like
 
John Ryan
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Cindy Glass:
So if you logged an error, why don't you just force open the Log file for the user and SHOW it to him? Or just write a message to the GUI or console telling him to go look at it.

Writing a message to the console is exactly what i want to do. I suppose as i am using log4j which can write to many locations i could use it to write the error to the console and not worry about returning the error message from the method..........
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah . . .
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic