0

So after beginning to understand why exception handling is required. It is also confusing to understand when to use try and catch and when to use throws. Both of them seems to do the same work but in different way. But while using the throws instead of try and catch. How does the exception gets handled. Why is it necessary to use throw Exception, when it doesn't really handles the exception. For example

public void method
{
try
{divide 1/0 }
catch(DivisionByZeroException e)
{do something }
}

AND 

public void method throws DivisonByZeroException
{
divide 1/0
}

whats the point of using throws in second method when it doesnt help the method at all and how does it get handled in the real world.

kds
  • 11
  • 1
  • 2
  • Possible duplicate of [Throws or try+catch](http://stackoverflow.com/questions/3203297/throws-or-trycatch) – Leo Nov 18 '15 at 03:19

1 Answers1

0

Throws is useful for reusable code, such as a library. It's like broadcasting "hey, an error happened" without deciding what to do about the exception. Try/catch is implemented to catch those thrown exceptions so you can decide what to do with it in your program.

JstnPwll
  • 8,585
  • 2
  • 33
  • 56