When a method throws and exception, do we need to have a try block inside the method?
For example,
public void foo() throws SomeException{
try{
// content of method
}
}
Is the try block required? Or, is the method able to throw a SomeException without it :
public void foo() throws SomeException{
// content of method
}
This is the case when we are not explicitly throwing a SomeException with throw.
If
SomeExceptionis a checked exception you have to eithertry{}catchblock orYou do not have to do both, either example you show in your question works just fine.
The difference is that with the
tryclause you handle theSomeExceptionyourself, whereas by declaring that your own methodthrowsit you delegate the responsability of handling theSomeExceptionto the calling method.