Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1) What is a Java exception and how do you program to handle them? 2) What is th

ID: 3795279 • Letter: 1

Question

1) What is a Java exception and how do you program to handle them?

2) What is the difference between the Error class and the Exception class?

3) What is a try-catch block? Can you have more than one catch block? Can try-catch blocks be nested?

4) What is the purpose of a Java throw statement?

5) What is a "throws clause" and where is it used?

6) Explain the concept of unreachable code in a Java program.

7) What is the purpose of a finally block?

8) What is a call stack and how can it be used when debugging?

9) What is the NullPointerException error in Java?

9) How can you create your own exceptions in a Java class?

10) What is a Java assertion and how are they used?

Explanation / Answer

Hi, I have answered the first 3 questions.

Please repost others in separate post.

Please let me know in case of any issue in first 3.

1) What is a Java exception and how do you program to handle them?
   An exception is an event, which occurs during the execution of a program, that disrupts the normal flow of the program's instructions.
   When an error occurs within a method, the method creates an object and hands it off to the runtime system. The object, called an exception object, contains information about the error, including its type and the state of the program when the error occurred. Creating an exception object and handing it to the runtime system is called throwing an exception.

   After a method throws an exception, the runtime system attempts to find something to handle it. The set of possible "somethings" to handle the exception is the ordered list of methods that had been called to get to the method where the error occurred.

   A method catches an exception using a combination of the try and catch keywords. A try/catch block is placed around the code that might generate an exception. Code within a try/catch block is referred to as protected code, and the syntax for using try/catch looks like the following

   Syntax
   try {
   // Protected code
   }catch(ExceptionName e1) {
   // Catch block
   }
   The code which is prone to exceptions is placed in the try block. When an exception occurs, that exception occurred is handled by catch block associated with it. Every try block should be immediately followed either by a catch block or finally block.

   Example
   The following is an array declared with 2 elements. Then the code tries to access the 3rd element of the array which throws an exception.

   import java.io.*;

   public class ExcepTest {

   public static void main(String args[]) {
   try {
   int a[] = new int[2];
   System.out.println("Access element three :" + a[3]);
   }catch(ArrayIndexOutOfBoundsException e) {
   System.out.println("Exception thrown :" + e);
   }
   System.out.println("Out of the block");
   }

2) What is the difference between the Error class and the Exception class?

   An Error "indicates serious problems that a reasonable application should not try to catch."

   while

   An Exception "indicates conditions that a reasonable application might want to catch."

   Exception and Error both are sub classes of java.lang.Throwable class.
   We can handle Exceptions at runtime but Errors we can not handle.

   Exceptions are the objects representing the logical errors that occur at run time and makes JVM enters into the state of "ambiguity".
   The objects which are automatically created by the JVM for representing these run time errors are known as Exceptions.
   An Error is a subclass of Throwable that indicates serious problems that a reasonable application should not try to catch. Most such errors are abnormal conditions.

3) What is a try-catch block? Can you have more than one catch block? Can try-catch blocks be nested?

   Java try block is used to enclose the code that might throw an exception. It must be used within the method.

   Java try block must be followed by either catch or finally block.

   Syntax of java try-catch

   try{
   //code that may throw exception
   }catch(Exception_class_Name ref){}

   Try defines a block of statements that may throw an exception. When a specific type of exception occurs, a catch block catches the exception. If an exception is not handled by try/catch blocks, the exception escalates through the call stack until the exception is caught or an error message is printed by the compiler.

   A try/catch block also may be nested with one or more try/catch statements. Each try statement has a matching catch statement to handle the exception. If an exception's inner try statement does not have a matching catch statement, subsequent try statement catch handlers are checked. This process continues until all inner try statements are checked for a matching catch statement. If a catch statement does not match, the runtime system handles the exception.

   Try/catch block examples include:

   A try block followed by a catch block
   A try block followed by one or more catch blocks
   A try block followed by another try block and then followed by a corresponding catch block