This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Exception handling and throwing

 
KM Chen
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • 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
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65105
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not an Ant question, is it? Moving this to the Java in General (beginner) forum.
 
Karthikeyan Rajendraprasad
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • 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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic