1.Can we write return statement in try or catch blocks in java
i) return statement in try block only
ii) return statement in try block and end of the method but after return one statement
Output:
Return statement in catch block:
i) return statement in catch block only
ii) return statement in catch block and end of the method but after return one statement
Output:
Return statement with try catch block
- Inside method if we have some statements which may proven to raise exceptions we need to keep those statements in side try catch blocks in order to handle the exceptions.
- There is a situation where a method will have a return type and we can return some value at any part of the method based on the conditions.
- So mixing these two scenarios we may have a situation to return some value from try block or catch block in such cases we need to follow some basic rules.
- This is also one of the famous interview question "can we write return statement in try block ", "can we write return statement in catch block ", "what will happen if we return some value in try block or catch block ", "will catch block execute after return statement " and "what are the basic rules we need to follow when we are returning or our method returning some value in try or catch blocks"
- We will see the all the scenarios and conclusion.
i) return statement in try block only
- If we return a value in try block and if we do not return a value at the end of the method it leads to compile time exception
- Error: This method must return a result of type int
- package com.exceptionhandlingiinterviewquestions;
- public class TryCatchReturn{
- int calc(){ // Error:This method must return a result of type int
- try {
- return 1;
- } catch (Exception e) {
- }
- System.out.println("End of the method");
- }
- public static void main(String[] args) {
- TryCatchReturn obj = new TryCatchReturn();
- }
- }
ii) return statement in try block and end of the method but after return one statement
- Even though it in this condition it wont reach end of the method still it asks us for return at end of the method because in some cases there may be a chance of getting exception in try block so it will not completely execute try in this case we don not have return in catch so at end of the method it expects some value to be return because the method have some return type.
- If we are keeping return statement in try block only there may be a situation of chance of raising exception and try will not execute completely and it goes to catch block that is the reason it expecting a return at catch or end of the method
- So in this scenario we will try to keep return but after return we have written one statement
- package com.exceptionhandlingiinterviewquestions;
- public class TryCatchReturn{
- int calc(){
- try {
- return 1;
- } catch (Exception e) {
- }
- return 10;
- System.out.println("End of the method"); // Error : Unreachable code
- }
- public static void main(String[] args) {
- TryCatchReturn obj = new TryCatchReturn();
- }
- }
- in the above program last statement will not execute at any condition so it became unreachable code as we know java does not supports unreachable codes so it will raise compile time error.
- This is the correct and successful scenario with respect to try with return statement in java
- Lest see a java program on this
- package com.exceptionhandlingiinterviewquestions;
- public class TryCatchReturn{
- int calc(){
- try {
- return 1;
- } catch (Exception e) {
- }
- System.out.println("End of the method");
- return 10;
- }
- public static void main(String[] args) {
- TryCatchReturn obj = new TryCatchReturn();
- System.out.println(obj.calc());
- }
- }
Output:
- 1
Return statement in catch block:
i) return statement in catch block only
- If we return a value in catch block and if we do not return a value at the end of the method it leads to compile time exception
- Error: This method must return a result of type int
- package com.exceptionhandlingiinterviewquestions;
- public class TryCatchReturn{
- int calc(){ // Error:This method must return a result of type int
- try {
- } catch (Exception e) {
- return 1;
- }
- System.out.println("End of the method");
- }
- public static void main(String[] args) {
- TryCatchReturn obj = new TryCatchReturn();
- }
- }
ii) return statement in catch block and end of the method but after return one statement
- package com.exceptionhandlingiinterviewquestions;
- public class TryCatchReturn{
- int calc(){
- try {
- } catch (Exception e) {
- return 1;
- }
- return 10;
- System.out.println("End of the method"); // Error : Unreachable code
- }
- public static void main(String[] args) {
- TryCatchReturn obj = new TryCatchReturn();
- }
- }
- in the above program last statement will not execute at any condition so it became unreachable code as we know java does not supports unreachable codes so it will raise compile time error.
- package com.exceptionhandlingiinterviewquestions;
- public class TryCatchReturn{
- int calc(){
- try {
- int x=12/0;
- } catch (Exception e) {
- return 1;
- }
- return 10;
- System.out.println("End of the method"); // Error : Unreachable code
- }
- public static void main(String[] args) {
- TryCatchReturn obj = new TryCatchReturn();
- System.out.println(obj.calc());
- }
- }
- 1
Return statement with try catch block
Bagikan
Return statement in try catch block java
4/
5
Oleh
Kris Kimcil