• Post Reply Bookmark Topic Watch Topic
  • New Topic

Exception  RSS feed

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
I have got a query on Exceptions.
It may sound a bit silly,but i had to ask this.

The exception code which we write is for the End User or the Programmer who use them/maintain them ?

I have this question because,we as programmers can use an "Exception" type directly and write the required code handling ,instead of specifically an IOException or a FileNotFoundException .


try{

//code
}catch(Exception e){

//code
}
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Keshav,

The exception code is for the Programmer so that he/she can take corrective action in case of problem. The corrective action may be sending some informative message to send user, closing database/io streams, resetting the values of variables etc.
 
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
keshav pradeep ramanath wrote:The exception code which we write is for the End User or the Programmer who use them/maintain them ?

A bit of both. In general, you don't want end users seeing exceptions unless they've done something really stupid, but you DO need a mechanism for understanding when an error has occurred.

I have this question because,we as programmers can use an "Exception" type directly...

Yes, and it's generally a bad way to code; Exceptions should be as explicit as possible. Unfortunately, the existence of "catch-all" exceptions like IOException and SQLException often make this very difficult to achieve, but that doesn't mean that you shouldn't try.

HIH

Winston
 
Ranch Hand
Posts: 449
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston answer is already very explanatory and clear . Just to give you an small example regarding why we not catch Exception directly, consider below code,

this code will catch every exception that will throw inside try block. It might be the case that you don't want to handle every error, for instance "OutOfMemory" error,

now, consider below sample code,
now suppose if you are trying to open a file but it doesn't exist, you will catch it and may create any default file or blank file.
 
keshav pradeep ramanath
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot folks.The explanations really helped!!
Cheers
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!