File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

doubt regarding try and catch

 
sudharshan tettu
Ranch Hand
Posts: 126
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 76
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 278
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic