*
The moose likes Beginning Java and the fly likes Exception handling and throwing Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Exception handling and throwing" Watch "Exception handling and throwing" New topic
Author

Exception handling and throwing

KM Chen
Greenhorn

Joined: Nov 02, 2004
Posts: 5
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

Joined: Jan 10, 2002
Posts: 60822
    
  65

Not an Ant question, is it? Moving this to the Java in General (beginner) forum.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Karthikeyan Rajendraprasad
Ranch Hand

Joined: Apr 16, 2003
Posts: 70
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 ]

Karthikeyan<br />SCJP 1.4, SCWCD.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Exception handling and throwing
 
Similar Threads
How to identify exception source?
Exception handling and throwing
Catch exceptions (where?)
Question on exception handling
exception declaration