aspose file tools*
The moose likes Java in General and the fly likes when it is required to handle exception inside the function ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "when it is required to handle exception inside the function ?" Watch "when it is required to handle exception inside the function ?" New topic
Author

when it is required to handle exception inside the function ?

Soumya Padhiary
Greenhorn

Joined: Jan 10, 2013
Posts: 20

When it is required to handle exception inside the same function(where exception arised) and inside the calling place ?
case 1:
class Excp{
void func()
{
...........
try{
.........
......... //Exception arised
.........
}catch(Exception e)
{}
.........
}
main(...){
Excp e=new Excp();
e.func();
}}

case 2:
class Excp{
void func()
{
..........
.......... //Exception arised
..........
}
main(.....){
Excp e=new Excp();
try{
e.func();
}catch()
{}
}}

Can any one tell me in which situation we go for case 1 and case 2 ?

Appreciation in advance .
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61418
    
  67

Under no circumstances should you ever have an empty catch block. Never. Even if you need to "eat" the exception under rare circumstances, there should be a long comment explaining why there is an empty catch block.

But to answer the general question, you only catch an exception at a level that you have something useful to do with it.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39396
    
  28
Or, to give the another answer to your original question: never.

If you throw any Exception in any method, you should pass that back to the calling method as a signal that something has gone wrong. Throwing an Exception and catching it in the same method is simply an expensive way to write if‑else.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: when it is required to handle exception inside the function ?