Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

exceptions

 
mrudul joshi
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
.exceptions are brodly classified into checked exceptions and unchecked exceptions, methods that throw unchecked exceptions are not obliged to handle those exceptions.but why should we need to throw such an exception that will cause our program to terminate?why are exceptions divided into checked and unchecked exceptions?and if a method throws checked exception but does not provide its handler, then also the program will terminate and if it throws an unchecked exception, then also the program will terminate.then why such distinction?
 
Wayne L Johnson
Ranch Hand
Posts: 399
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I suppose that since there is additional overhead involved when you put code into a try/catch block, the language designers decided they would allow for unchecked exceptions. These may cause your application to terminate, but you don't incur the overhead of checking for every possible exception that could happen.
For example, if you check an index before accessing an array, you'll never get an "ArrayOutOfBounds" exception. I'd hate to have to put every single array access inside of a "try/catch" block because some designer decided to make it a checked exception. As a programmer you have complete control over this behavior.
However when you are trying to make a socket connection, you may run into network traffic, or the remote server may be down, or it may have hit the maximum number of connections allowed. These are all conditions that you have no control over, so it makes more sense to throw checked exceptions that force you to handle these situations.
That leaves it up to the various class writers to decide whether they want to use checked or unchecked exceptions. In some cases it boils down to a philosophical discussion of which one to use when. If you google on "Java checked unchecked exception" you'll find a lot of different opinions regarding this issue.
[ November 13, 2003: Message edited by: Wayne L Johnson ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic