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
  • Exception handling All Exceptions that can be thrown must descend from this Java class: The getMessage(...

    Exception handling All Exceptions that can be thrown must descend from this Java class: The getMessage( ) method is inherited from this class? What are the two broad catagories of Exceptions in Java? If an Exception is not a checked exception it must extend what class? What is the difference between a checked Exception and an unchecked Exception? What are the two options a programmer has when writing code that may cause a checked Exception to be thrown? Can a...

  • True or False As it pertains to Exception handling in Java, the finally block gets executed...

    True or False As it pertains to Exception handling in Java, the finally block gets executed only if no exception is caught.

  • (Python) Exercise-1: Raising Exceptions Try the following code one by one: raise NameError(‘Name not Found’) raise...

    (Python) Exercise-1: Raising Exceptions Try the following code one by one: raise NameError(‘Name not Found’) raise IndexError("Sorry, index not found!") raise KeyError("Sorry, my fault!") What it does? Exercise-2: Raising Exceptions Try the following code: try : raise NameError( 'Name not found' ) except NameError: print ( 'caught it' ) What it does? Exercise-3: Raising Exceptions using class Here is an example related to RuntimeError. Here, a class is created that is a sub-class from RuntimeError. This is useful when you...

  • When using the Java mail API, the MessagingException is often caught in the catch block rather...

    When using the Java mail API, the MessagingException is often caught in the catch block rather than other Java exceptions that are related to the use of the Java mail API. Why is this?

  • For practice using exceptions, this exercise will use a try/catch block to validate integer input from...

    For practice using exceptions, this exercise will use a try/catch block to validate integer input from a user. Write a brief program to test the user input. Use a try/catch block, request user entry of an integer, use Scanner to read the input, throw an InputMismatchException if the input is not valid, and display "This is an invalid entry, please enter an integer" when an exception is thrown. InputMismatchException is one of the existing Java exceptions thrown by the Scanner...

  • 3. Handling exceptions with a finally clause a. Download the following file from the class website:...

    3. Handling exceptions with a finally clause a. Download the following file from the class website: TestFinally.java b. Examine the code to determine what it does. c. Compile the code and notice the error. d. Modify TestFinally.java to handle the exception following these instructions (and those embedded in the code): i. Embedded in the code is a note showing the location for the try statement. ii. The first line of the catch block should look like this: catch(IOException e) {...

  • -------------------------------------------------------------------- 1. How does Java support the concept of encapsulation? -------------------------------------------------------------------- 2. Describe the difference between...

    -------------------------------------------------------------------- 1. How does Java support the concept of encapsulation? -------------------------------------------------------------------- 2. Describe the difference between an object and a class. -------------------------------------------------------------------- 3. What is the difference between the contents of a Java variable of a primitive type and a Java variable of a class type? -------------------------------------------------------------------- 4. (a) How is a 'static' class method different from a regular (non-static) class method? (b) How is a 'static' variable in a class different from a regular (instance) variable in a class?...

  • Question 1 (5 points) Question 1 Unsaved What is displayed on the console when running the...

    Question 1 (5 points) Question 1 Unsaved What is displayed on the console when running the following program? public class Quiz2B { public static void main(String[] args) { try { System.out.println("Welcome to Java"); int i = 0; int y = 2 / i; System.out.println("Welcome to Java"); } catch (RuntimeException ex) { System.out.println("Welcome to Java"); } finally { System.out.println("End of the block"); } } } Question 1 options: The program displays Welcome to Java two times. The program displays Welcome to...

  • Introduction In this lab we will look at the information given by thrown exceptions. In particular,...

    Introduction In this lab we will look at the information given by thrown exceptions. In particular, we will see that a given design does not give the most useful information that it could. We will explore how to fix it. The Program We have three classes in the project. StringArray is similar to the class in the homework, with much functionality removed. Names is meant to be a collection of names that has a StringArray used to store them. It...

  • How would I alter this code to have the output to show the exceptions for not...

    How would I alter this code to have the output to show the exceptions for not just the negative starting balance and negative interest rate but a negative deposit as well? Here is the class code for BankAccount: /** * This class simulates a bank account. */ public class BankAccount { private double balance; // Account balance private double interestRate; // Interest rate private double interest; // Interest earned /** * The constructor initializes the balance * and interestRate fields...

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