Yes, we can write the return statement of the method in the catch and finally blocks.
public class CatchReturn { int calc() { try { int x=12/0; } catch (Exception e) { return 1; } return 10; } public static void main(String[] args) { CatchReturn cr = new CatchReturn(); System.out.println(cr.calc()); } }
1
public class FinallyReturn { int calc() { try { return 10; } catch(Exception e) { return 20; } finally { return 30; } } public static void main(String[] args) { FinallyReturn fr = new FinallyReturn(); System.out.println(fr.calc()); } }
30
The above is the detailed content of In Java, can we use return statement in catch or finally block?. For more information, please follow other related articles on the PHP Chinese website!