This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes doubt regarding try and catch 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 "doubt regarding try and catch" Watch "doubt regarding try and catch" New topic
Author

doubt regarding try and catch

sudharshan tettu
Ranch Hand

Joined: Jul 17, 2006
Posts: 124
i have two methods....m1() and m2()...

m1()
{

m2();
}


m2() throws SQLException,ClassNotFoundExcepion
{
-----
try {
...........
........
}
catch(SQLException slq)
{}
catch(ClassNotFoundExcepion clf){}

}


when i try calling m2 from inside m1 and m2 ..its throwing error like uncaught excecpiton SQLExcepion and ClassNotFoundExcepion inside m1..
could anyone explain i am rather new to this
Srikanth Ramu
Ranch Hand

Joined: Feb 20, 2007
Posts: 76
since your m2() throws two Exceptions you need to handle them in m1() too.

In your case you can handle the exception in one method either m1() or m2(). If you want to handle exceptions in m1() then
avoid catching them in m2(). If you want to handle the exceptions in m2() then avoid throwing.

However the above suggestions are subjective and Exception handling should be done based on your requirment.
ankush dixit
Greenhorn

Joined: Mar 14, 2006
Posts: 4
i�m not sure what�s in you m2 method but since you are explicitly handling SQLException and ClassNotFoundExcepion in your m2 method so there is no need to declare them in throws clause...

just remember that every method should catch and handle all checked exception or throw it to the calling method which should do the same...
Lucky J Verma
Ranch Hand

Joined: Apr 11, 2007
Posts: 278
A method ,if can be causing an exception ,
1.should either handle the exception(Using try-catch) or
2.atleast must pass it to its calling method(Using throws clause)

No matter ,we can do both.

Now in your code, m2() is catching the exception itself
by introducing try-catch blocks .

But by writing ,throws clause(which wasnt required)
m1()-caller method of m2() ,thinks ,m2() may cause some exception in future .

so again m1() has to handle it with try-catch or pass it to caller.

With your program,remove ,try-catch or throws in m2() OR
add try-catch or throws in m1()
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: doubt regarding try and catch
 
Similar Threads
Class level lock versus Object level lock
Exceptions and interfaces
Passing Variables into Methods concept
multiple synchronized block in class..
exception dilema