Page 1 of 1

[HD Pg 374, Sec. 10.5.0 - exercises]

Posted: Sat Jun 08, 2024 3:03 pm
by raphaelzintec
weird, end of the exception chapter and have nothing seen about return in try/catch/finally

gotta learn it by myself trough tests and here i'm sharing:

RETURN "2" from try cuz no exceptions

Code: Select all

    static String m(){
        try {
            return String.valueOf(5/2);
        }   catch (Exception e){
            return "catch";
        }
    }
RETURN "finally" because it overrides the try return

Code: Select all

    static String m(){
        try {
            return String.valueOf(5/2);
        }   catch (Exception e){
            return "catch";
        }
        finally {
            return "finally";
        }
    }
RETURN "finally" because it overrides the catch return

Code: Select all

    static String m(){
        try {
            return String.valueOf(5/0);
        }   catch (Exception e){
            return "catch";
        }
        finally {
            return "finally";
        }
    }
HERE it's very special, RETURN "finally" without even showing catch exception and program doesn't interrupt

Code: Select all

    static String m(){
        try {
            return String.valueOf(5/0);
        }   catch (Exception e){
            throw new Exception();
        }
        finally {
            return "finally";
        }
    }
Another interesting thing is that if i have a throw for all then compiler won't complain for a missing return statement. It will only complain if catch/finally omit them

Code: Select all

    static String m(){
        try {
            throw new RuntimeException();
        }   catch (Exception e){
            throw new RuntimeException();
        } finally {
            throw new RuntimeException();            
        }
    }

Re: [HD Pg 374, Sec. 10.5.0 - exercises]

Posted: Sat Jun 08, 2024 3:15 pm
by raphaelzintec
it's very interesting to know that a return in finally overrides the throw in catch so program won't interrupt

but in a void method the throw will be executed after finally and stopt the program

CODE

Code: Select all

public static void main(String[] args) throws IOException {
        System.out.println(m());
        System.out.println("m end");
        m2();
        System.out.println("m2 end");
    }

    static String m(){
        try {
            return String.valueOf(5/0);
        }   catch (Exception e){
            throw new RuntimeException();
        }
        finally {
            return "finally m";
        }
    }

    static void m2(){
        try {
            System.out.println(String.valueOf(5/0));
        }   catch (Exception e){
            throw new RuntimeException();
        }
        finally {
            System.out.println("finally m2");
        }
    }
PRINTS

Code: Select all

finally m
m end
finally m2
Exception in thread "main" java.lang.RuntimeException
	at exceptions.B.m2(B.java:30)
	at exceptions.B.main(B.java:11)

Re: [HD Pg 374, Sec. 10.5.0 - exercises]

Posted: Sat Jun 08, 2024 10:08 pm
by admin
You are right, although not required for 1Z0-811, 1Z0-808, and 1Z0-815(which is discontinued now) exams, this aspect should nevertheless be included in the book.
Thank you for your feedback!