Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

why to remember names of specific exceptions??

 
Punit Jain
Ranch Hand
Posts: 1015
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why it's necessary to remember name of exceptions, although in our catch block we can directly put Exception.



than why to put specific type of exception in catch block??
 
E Armitage
Rancher
Posts: 989
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To do something different for different types of exceptions.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15484
43
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java has checked exceptions: if you have some code that can throw some specific kind of exception and you call it, the compiler will check that you properly handle the exception, either by catching it or by adding a 'throws' clause to your method to indicate that your method can throw the exception further.

If you catch (Exception e) then you are going against the whole idea of checked exceptions. You just catch any checked exception - it indicates that you haven't thought very hard about how to handle different kinds of possible errors. If you're just catching everything, then what sense does it make for the compiler to check that you're carefully handling any of the possible exceptions?
 
Stephan van Hulst
Bartender
Posts: 6323
78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because you will be catching much more than you should if you do that. You should only catch exceptions that you wish you handle, and let the rest propagate upwards through the stack. You should very rarely have any need to handle RuntimeExceptions and Errors, so catching Exception (or Throwable) is almost always inappropriate.
 
David Kay
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Say you had a method that reads data in from a file and returns the data. If you wrap a call to that function in a generic try...catch(Exception e) then you wont know what went wrong. Whereas if you try to catch specific exceptions, FileNotFoundException for example, then you can act on this information by informing the user that the file doesn't exist, rather than a more generic error message.
 
Daniel Renner
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But in some cases it is ok to catch an exception in your way.
Example:
In development mode GWT throws a NullPointerException, while in productive mode a GwtException appears. So this is hard to handle, cause the easiest thing is to catch the parent "Exception" to catch both of the exceptions.

But this is a special case, in prefer to catch the specific exception(s). Often you need to distinguish between the different exceptions, f.e. to retry, close connection, ... .

In other cases it is possible that you catch an exception which you dont want do catch (cause the program cant accomplish any other task with this error/exception).
 
Punit Jain
Ranch Hand
Posts: 1015
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so if i catch Exception instead of any specific exception, is it wastage of resource??
(ie. memory), isn't it??
 
Manoj Kumar Jain
Ranch Hand
Posts: 198
Java Linux Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's not related to any wastage or utilization of the resource, this is all about the logic whether you are handling these special events(exceptions) and how you are handling.
You might want to retry or pass through different branch if there is a particular exception, I can remember one case where we were parsing the XML/SOAP. But we didn't know that the input will be simple XML or a SOAP. So what we did we just tried to parse it as SOAP and if we get parsing exception then we assume it as a simple XML and proceeded successfully.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong wrote:Java has checked exceptions: if you have some code that can throw some specific kind of exception and you call it, the compiler will check that you properly handle the exception, either by catching it or by adding a 'throws' clause to your method to indicate that your method can throw the exception further.

If you catch (Exception e) then you are going against the whole idea of checked exceptions. You just catch any checked exception


And you also catch one of the two major subtrees of unchecked exceptions. Bad idea in most cases.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Punit Jain wrote:so if i catch Exception instead of any specific exception, is it wastage of resource??
(ie. memory), isn't it??


No, it doesn't waste anything. The reasons to not just catch Exception are:

1) It takes away visibility and control over which exceptions are being caught. You don't get to handle different kinds of exceptions differently (unless you do something like instanceof inside your catch block, which would be icky).

2) If the code you're calling gets upgraded and starts throwing a different set of checked exceptions, the compiler won't be able to tell you about it. (This is, admittedly, a minor point, but it bit me once, and I'll never forget it.)

3) Catching Exception includes RuntimeException and all its descendants, and we usually DON'T want to catch those.
 
Paul Clapham
Sheriff
Posts: 21416
33
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It used to be that if you were writing code which used certain complex packages and which worked with networks that you ended up with code like this:


I think my record was six different exceptions which had to be caught. In such a case there was a great temptation to rationalize that code to make it look simpler:


But now in Java 7 you can catch several different exceptions in one catch-clause:
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic