Question

Show how exceptions are thrown and caught in Java. When does the finally block get executed?...

Show how exceptions are thrown and caught in Java. When does the finally block get executed? Show with a sample code if the finally block is executed if we have a return statement in the try block.write code in java

0 0
Add a comment Improve this question Transcribed image text
Answer #1
//Divide.java
public class Divide {

    public static double divide(double a, double b){
        double c = 0.0;
        try{
            c = a/b;
            return c;
        }
        catch (ArithmeticException ae){
            System.out.println("ArithmeticException");
        }
        finally {
            //This block runs every time even exception caught
            System.out.println("Running finally block");
            return c;
        }
    }

    public static void main(String[] args){
        System.out.println(divide(2,0));
        System.out.println(divide(2,5));

    }
}

Running finally block Infinity Running finally block 0.4 Process finished with exit code 0

\color{red}Please\; upvote\;the \;solution \;if \;it \;helped.\;Thanks!

Add a comment
Know the answer?
Add Answer to:
Show how exceptions are thrown and caught in Java. When does the finally block get executed?...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT