[HD Pg 374, Sec. 10.5.0 - exercises]

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
raphaelzintec
Posts: 167
Joined: Sun Apr 21, 2024 10:43 am
Contact:

[HD Pg 374, Sec. 10.5.0 - exercises]

Post 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();            
        }
    }
Last edited by admin on Sun Jun 09, 2024 12:08 am, edited 2 times in total.
Reason: Please put code inside [code] [/code]

raphaelzintec
Posts: 167
Joined: Sun Apr 21, 2024 10:43 am
Contact:

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

Post 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)
Last edited by admin on Sun Jun 09, 2024 12:09 am, edited 1 time in total.
Reason: Please put code inside [code] [/code]

admin
Site Admin
Posts: 10386
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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!

Post Reply

Who is online

Users browsing this forum: No registered users and 5 guests