Reputation: 390
I have a simple coding scenario like :
class A
{
public static void main(String[] args)
{
try{
//some exception
}
catch(Exception e)
{
//Again some exception
}
finally
{
System.out.println("Finally executed");
}
}
}
My question:
Upvotes: 5
Views: 109
Reputation: 96385
1 and 2) yes, you can do both of these by nesting try blocks or letting the exception get thrown (depending on what the situation requires).
3) it is not bad practice, it is unavoidable
There are a couple of things you should be beware of.
One potential problem is exception-masking. When you throw an exception from a catch or finally block, that will supercede any original exception. If you have some kind of Closeable like an InputStream or JDBC object, where you have to close the resource in a finally block, if you let that exception get thrown then you lose the original exception that would tell you what actually went wrong.
Another thing to consider, for the case where you have an exception thrown in a finally block, is whether you want the exception to get propagated at all. For the JDBC example, once a transaction is already committed then if there's an exception thrown on closing the connection it's irrelevant to the business logic, it's just a nuisance. So a common approach for that kind of thing is to catch the exception in the finally block and log it, but don't rethrow it.
The try-with-resources feature is an attempt to fix exception-masking. If an exception is thrown in a finally block after an exception is thrown in the try, the exception on close is added onto the original exception and you can access it through the Throwable#getSuppressed
method.
try-with-resources also provides a way to avoid nesting of try-finally blocks, you can declare multiple closeables and they will get closed in order, last-in-first-out.
try-with-resources suppression of exceptions thrown on close only works if you have an exception thrown in the try block, otherwise the exception thrown on close gets propagated. This behavior is different from the old pattern of catching and logging anything thrown on close, which prevents the close exception from getting rethrown at all.
Upvotes: 1
Reputation: 1596
1) Is there any way to handle exception in catch ? if yes,then how ? Yes
You can use another try catch
block inside catch()
or you can also throw an exception;
try {
// some exception
}
catch (Exception e) {
try {
// Again some exception
// some exception
}
catch (Exception e1) {
// Again some exception
throw new RuntimeException();//throwing a runtime exception, you can throw here your required exception
}
}
2) What if finally block have exception,Any way to handle them ? Yes The same way I discussed for question (1).
3) Or Is it only bad programming practice to have exception in catch or finally block ? May be
Because you need to catch all the exception inside the catch block and It is the better way to do. But you can handle the exception in finally
block by using another try catch
block.
Sample code for finally block in Try catch block
finally {
try {
System.out.println("Finally executed");
} catch (Exception final_ex) {
// handle exception
}
}
Upvotes: 1
Reputation: 464
You can simply handle them following way...
class A
{
public static void main(String excep[])
{
try{
//some exception
}
catch(Exception e)
{
//Again some exception
try {}
catch(Exception e){}
}
finally
{
System.out.println("Finally executed");
try {}
catch(Exception e){}
}
}
}
this is usually done in FileStream closing example as shown below example
FileStream in = new FileStream(f);
finally {
if (input != null) {
try {
in.close();
}catch (IOException exp) {
System.out.println(exp);
}
}
Upvotes: 2
Reputation: 2887
Its not bad practice but if you want to handle exception in a meaningful way catch the expected exception. Example is below.
public void test()
{
InputStream stream = null;
try{
// do something
// initialize stream
// again do something
}
catch(ExpectedException e)
{
//Handle the expected exception add a meaningful message here
//If needed new exception (custom exception) can be thrown to where this
// method is called and the new exception should be a meaningful to the called
}
finally
{
if (stream != null)
try{
steam.close();
}catch(Exception e){
//log the error and throw if needed as explained above.
}
}
}
Upvotes: 0