• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Exception handling and throwing

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Is any effeciency difference between the following 2 approaches for Exception Handling?
approach1:
method1()
{
try{
method2();
method3();
}
catch (Exception2 e2)
{
// handle Exception2
}
catch (Exception3 e3)
{
// handle Exception3
}
catch (Exception e)
{
// handle Exception
}

}

void method2() throws Exception2
{
// do something here
}

void method3() throws Exception3
{
// do something here
}


approach2:
method1()
{
try{
method2();
method3();
}
catch (Exception e)
{
// handle Exception
}

}

void method2() throws Exception2
{
try
{
// do something here
}
catch (Exception2 e2)
{
// handle Exception2
}

}

void method3() throws Exception3
{
try
{
// do something here
}
catch (Exception3 e3)
{
// handle Exception3
}

}


Thanks,
Kang
 
Sheriff
Posts: 67747
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not an Ant question, is it? Moving this to the Java in General (beginner) forum.
 
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Normally it is advisable to bubble out the exception and handle it at the calling point. in your case the approach one is advisable because the caller comes to know about the exception, where as in the second one the caller does not know what happened.
in approach 2 you handle the exception you catch and if you dont throw it again the catch it will result in an compilation error as you still say the method throws thw exception
[ November 02, 2004: Message edited by: Karthikeyan Rajendraprasad ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic