Question

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 try block contain code that would cause more than one type of Exception to possibly be thrown? Is it true that a try block can only be followed by ONE catch block? What is the purpose of the finally block? When is the code in the finally block executed? What happens when a try block throws an Exception object? Is an Exception the same thing as a syntax or logic error?

0 0
Add a comment Improve this question Transcribed image text
Answer #1

(a) Throwable class is the superclass from which all exceptions are derived.

(b) There are two types of exceptions in Java - Checked exceptions and unchecked excpetions.

(c) Unchecked exceptions extend either the Error class or the RuntimeException class.

(d) Checked exceptions are checked at compile time whereas unchecked exceptions are not.

(e) If a programmer is writing code that may cause a checked exception to be thrown, his method must either handle the exception or he must declare the exception using the throws keyword.

(f) Yes, a try block can have code that can cause more than one exception to be thrown.

(g) No, a try block can be followed by multiple catch blocks. Like so,

try{
         // Some code here
     }
     catch(ArithmeticException e){
        //
     }
     catch(ArrayIndexOutOfBoundsException e){
        //
     }
     catch(Exception e){
        //
     }

(h) The purpose of the finally block is to contain all those statements that have to be executed irrespective of whether an exception occurs or not.

(i) If there is no exception, then the code in the finally block is executed after the execution of the try block. However, if an exception occurs then the finally block is executed after the execution of the catch block.

(j) When a try block throws an exception it is caught by its corresponding catch block.

(k) An Exception can be caught by the program and handled accordingly. The same is not true for an Error. Errors are unchecked Exceptions.

Add a comment
Know the answer?
Add Answer to:
Exception handling All Exceptions that can be thrown must descend from this Java class: The getMessage(...
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
  • Code that might cause an exception to be thrown is placed in a ____ block; code...

    Code that might cause an exception to be thrown is placed in a ____ block; code that processes the exception is placed in a ____ block. a) checked, unchecked b) catch, finally c) try, catch d) input, output

  • 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

  • Select the correct statement(s): 1)Exceptions are compiler errors 2) Exceptions can be thrown from a method....

    Select the correct statement(s): 1)Exceptions are compiler errors 2) Exceptions can be thrown from a method. The caller of the method can catch and handle the exception. 3) The Throwable class is not the root of exception classes. 4) RuntimeException, Error, and their subclasses are known as unchecked exceptions.

  • 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) {...

  • Please answer both questions thanks Question 16 (1 point) Select all of the TRUE statements. A...

    Please answer both questions thanks Question 16 (1 point) Select all of the TRUE statements. A method has at least one return statement. A method has at most one return value. When executing a return statement, the method exits immediately. A method with return value void must print a result. A method can have multiple return statements. Question 17 (1 point) What is the difference between a checked exception and an unchecked exception? A checked exception indicates ndicates an error...

  • 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...

  • Advanced Object-Oriented Programming using Java Assignment 4: Exception Handling and Testing in Java Introduction -  This assignment...

    Advanced Object-Oriented Programming using Java Assignment 4: Exception Handling and Testing in Java Introduction -  This assignment is meant to introduce you to design and implementation of exceptions in an object-oriented language. It will also give you experience in testing an object-oriented support class. You will be designing and implementing a version of the game Nim. Specifically, you will design and implement a NimGame support class that stores all actual information about the state of the game, and detects and throws...

  • import java.util.Scanner; import java.io.File; public class Exception2 {        public static void main(String[] args) {              ...

    import java.util.Scanner; import java.io.File; public class Exception2 {        public static void main(String[] args) {               int total = 0;               int num = 0;               File myFile = null;               Scanner inputFile = null;               myFile = new File("inFile.txt");               inputFile = new Scanner(myFile);               while (inputFile.hasNext()) {                      num = inputFile.nextInt();                      total += num;               }               System.out.println("The total value is " + total);        } } /* In the first program, the Scanner may throw an...

  • (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...

  • MUST BE IN C++ The lab is called "7.3.7.1 Exceptions: file checks" The lab is as...

    MUST BE IN C++ The lab is called "7.3.7.1 Exceptions: file checks" The lab is as follows: Objectives Familiarize the student with: situations when exceptions are thrown; handling file-related exceptions. Scenario Write a class that holds a (2×2) matrix, and add two methods to work with files: one method to load the matrix from a file (in any format) and one method to save the matrix to a file (in the same format). Add code to handle exceptional situations (file...

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