If a catch throws exceptions, how should I handle them?

I have a simple coding script, for example:

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 is:

  • Is there a way to handle an exception in catch? if so, how?
  • What if, finally, the block has an exception, is there a way to handle them?
  • Or is it just bad programming practice to have an exception in catch or finally block?
+4
source share
4 answers

1 and 2) yes, you can do both of them by inserting try blocks or by allowing an exception (depending on the situation).

3) this is not a bad practice, it is inevitable

There are a few things to watch out for.

- . catch finally, . - Closeable, InputStream JDBC, finally, , , , .

, , , , finally, , , . JDBC, , , , -, . , , finally , .

try-with-resources - . finally , try , , Throwable#getSuppressed.

try-with-resources try-finally, , , -.

try-with-resources , , , try , . , , .

+1

...

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){}
        }
    }
}

FileStream, .

FileStream in = new FileStream(f);
finally {
if (input != null) {
   try {
     in.close();
   }catch (IOException exp) {
       System.out.println(exp);
    }
}
+2

1) catch? , ?

try catch catch() ;

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) , , ? (1).

3) , catch , , ?

catch, . finally, try catch.

finally Try catch

        finally {
            try {
                System.out.println("Finally executed");
            } catch (Exception final_ex) {
                // handle exception 
            }
        }
+1
  • catch.
  • , .
  • Not a bad practice, but if you want to handle the exception in a meaningful way, understand the expected exception. An 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. 
          }
        }
    

    }

0
source

All Articles