1

I have just asked one question here.

It is about handling exception in a dummy Library management condition. The answers on the post are convincing that I should not go for Exceptions.

But then I just read article here which very well states that and which adheres to answers given on my earlier question

You should only create a new exception if you expect developers to take corrective action for the problem or to log for post mortem debugging.

As we should write them to assist developers not users, does this means that Exception have their place only in libraries that will be used by developers? , and if the code is not used by other developer then we should not use any exception in it?

If this is not the case, then can anyone tell me any instance that I may need to/should use Exception in Library management project, so that I can get an idea about when I should write Exception in code which are not meant to be called by another code.

I want to focus on when and where should I write Exceptions and especially if only in Libraries.

Community
  • 1
  • 1
Mahesha999
  • 22,693
  • 29
  • 116
  • 189
  • 1
    What's non-library code? 90% of my code, even when never decoupled from the application and/or used in more than one project, is effectively written and used like a library for the application. –  Jan 01 '13 at 07:06
  • I meant to say one which is not going to be used by another developer or more specifically the code that will not be called by another code. – Mahesha999 Jan 01 '13 at 07:08
  • As I said, I have a lot of code that's not used by "another developer" (or rather, "a developer not working on this project"). I don't have a lot of code that isn't called by other code if I take "other code" to mean "code outside the module in question", and that code rarely does anything that could raise an exception (except of course calling code which doesn't fall under these criteria). So I'm still not sure if I see the point of the question. Or do you mean "code not belonging to this project", i.e. third party code? That strikes me as a rather narrow definition. –  Jan 01 '13 at 07:17
  • well sorry if I meant it wrong or if it makes absolutely no sense, but the decision to use Exception is still very fuzzy to me. – Mahesha999 Jan 01 '13 at 07:23

1 Answers1

1

That is perfectly fine.

  1. Any library should not throw exception (checked exception), for the calling code. It should be handled properly by library itself.

  2. Regarding RunTimeExceptions, it depends on design of application/library. If user can/should take any corrective measures then we should throw RuntimeExceptions else not, just log them for developer.

Shiplu Mokaddim
  • 56,364
  • 17
  • 141
  • 187
Prateek
  • 523
  • 2
  • 13