5.7) What happens if a finally block modifies the value returned from a catch block?
Previous5.6) What happens if both a catch and a finally block define return statement?Next5.8) Can a try block be followed only by a finally block?
Last updated
Last updated
public class MultipleReturn {
int getInt() {
int returnVal = 10;
try {
String[] students = { "Harry", "Paul" };
System.out.println(students[5]);
} catch (Exception e) {
System.out.println("About to return :" + returnVal);
return returnVal;
} finally {
returnVal = returnVal + 10;
System.out.println("Return value is now :" + returnVal);
}
return returnVal;
}
}
public class TestMultipleReturn {
public static void main(String[] args) {
MultipleReturn var = new MultipleReturn();
System.out.println("In main : " + var.getInt());
}
}
public class MultipleReturn {
StringBuilder getStringBuilder() {
StringBuilder returnVal = new StringBuilder("10");
try {
String[] students = { "Harry", "Paul" };
System.out.println(students[5]);
} catch (Exception e) {
System.out.println("About to return :" + returnVal);
return returnVal;
} finally {
returnVal.append("10");
System.out.println("Return value is now :" + returnVal);
}
return returnVal;
}
}
public class TestMultipleReturn {
public static void main(String[] args) {
MultipleReturn var = new MultipleReturn();
System.out.println("In main : " + var.getStringBuilder());
}
}
public class MultipleReturn {
StringBuilder getStringBuilder() {
StringBuilder returnVal = new StringBuilder("10");
try {
String[] students = { "Harry", "Paul" };
System.out.println(students[5]);
} catch (Exception e) {
System.out.println("About to return :" + returnVal);
return returnVal;
} finally {
returnVal = new StringBuilder("1010");
System.out.println("Return value is now :" + returnVal);
}
return returnVal;
}
}
public class TestMultipleReturn {
public static void main(String[] args) {
MultipleReturn var = new MultipleReturn();
System.out.println("In main : " + var.getStringBuilder());
}
}
public class MultipleReturn {
String getString() {
String returnVal = new String("10");
try {
String[] students = { "Harry", "Paul" };
System.out.println(students[5]);
} catch (Exception e) {
System.out.println("About to return :" + returnVal);
return returnVal;
} finally {
returnVal.concat("101");
System.out.println("Return value is now :" + returnVal);
}
return returnVal;
}
}
public class TestMultipleReturn {
public static void main(String[] args) {
MultipleReturn var = new MultipleReturn();
System.out.println("In main : " + var.getString());
}
}
package com.gs.ilp.corejava.excpetionHandling;
class Rx {
int id;
String name;
}
public class TestReturn1 {
public static void main(String[] args) {
// int i = m1();
// System.out.println(i);
StringBuilder name = m2();
System.out.println("In main :" + name);
//Rx c = m3();
//System.out.println(c.id);
}
private static int m1() {
int i = 8;
try {
System.out.println(3 / 0);
} catch (ArithmeticException e) {
i = 18;
return i;
} finally {
i = 28;
System.out.println("Will I execute and what is the value of i in finally : " + i);
}
i = 23;
return i;
}
private static StringBuilder m2() {
StringBuilder i = new StringBuilder("Mohit");
try {
System.out.println(3 / 0);
} catch (ArithmeticException e) {
i.append("Malhotra");
return i;
} finally {
i = new StringBuilder(i);
i.append("Mehra");
System.out.println("Will I execute and what is the value of i in finally : " + i);
}
return i;
}
private static Rx m3() {
Rx c = new Rx();
c.id = 24;
c.name = "Mogit";
try {
System.out.println(3 / 0);
} catch (ArithmeticException e) {
c.id = 25;
return c;
} finally {
c.id = 26;
System.out.println("Will I execute and what is the value of i in finally : " + c.id);
}
return c;
}
}
Will I execute and what is the value of i in finally : MohitMalhotraMehra
In main :MohitMalhotra