Java Exception Handling
20 Questions
0 Views

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to Lesson

Podcast

Play an AI-generated podcast conversation about this lesson

Questions and Answers

What is the primary characteristic of an exception in programming?

  • It automatically resolves all errors in the code
  • It prevents the creation of objects during runtime
  • It is an event that disrupts the normal flow of program execution (correct)
  • It enhances the normal flow of program execution

When an exception occurs, no object is created to hold information about the error.

False (B)

What term describes the creation of an exception object and its transfer to the Java runtime system?

throwing an exception

______ exceptions are those that must be handled in order for the program to compile.

<p>Checked</p> Signup and view all the answers

Which of the following is an advantage of handling FileNotFoundException?

<p>It enables the program to prompt for a different file or end gracefully. (A)</p> Signup and view all the answers

Unchecked exceptions must be addressed in order to compile.

<p>False (B)</p> Signup and view all the answers

What is the primary reason the compiler does not force the handling of unchecked exceptions?

<p>The compiler is unable to anticipate these logical errors</p> Signup and view all the answers

An ArrayIndexOutOfBoundsException is an example of an ______ exception.

<p>unchecked</p> Signup and view all the answers

What is the key difference between exceptions and errors in Java?

<p>Exceptions are recoverable; errors are not. (A)</p> Signup and view all the answers

Exceptions and errors are unrelated concepts and do not share a common inheritance hierarchy

<p>False (B)</p> Signup and view all the answers

What two actions must a program take when an checked exception could occur?

<p>include a <code>throws</code> clause or include a <code>try-catch</code> structure</p> Signup and view all the answers

The ______ block in a try-catch structure contains code that will always be executed after the try and catch blocks, regardless of whether an exception was thrown.

<p>finally</p> Signup and view all the answers

Match each code block with its function in exception handling:

<p>try = Encloses code that might throw an exception catch = Handles the exception if one is thrown finally = Executes code regardless of whether an exception is thrown</p> Signup and view all the answers

In the context of the ReadBinary.java example, what happens when an IOException occurs and is not caught within a try-catch block?

<p>The program terminates abnormally. (D)</p> Signup and view all the answers

Declaring throws IOException in a method signature means the method must handle the exception internally.

<p>False (B)</p> Signup and view all the answers

In the HandleFileNotFound.java code, what output is printed to the console if the specified file is successfully found?

<p>Success, file found</p> Signup and view all the answers

In the HandleFileNotFound.java example, the DataInputStream is initialized to ______ before the try block.

<p>null</p> Signup and view all the answers

What exception might readInt() throw?

<p>Both <code>EOFException</code> and <code>IOException</code> (C)</p> Signup and view all the answers

It is generally recommended to create custom exceptions to handle specific application-level problems.

<p>False (B)</p> Signup and view all the answers

If a custom checked exception is required, what class should the custom exception extend?

<p>exception</p> Signup and view all the answers

Flashcards

What is an exception?

An event during program execution that disrupts the normal flow.

What are checked exceptions?

Exceptions that must be handled in order for the code to compile.

What are unchecked exceptions?

Exceptions that do not need to be addressed in order to compile.

What is a try-catch?

A block of code used to handle potential exceptions.

Signup and view all the flashcards

What is 'finally' block?

Section of code that always executes, whether exception happens or not.

Signup and view all the flashcards

What is custom exception?

A class that extends Exception or RuntimeException.

Signup and view all the flashcards

What is Exception Handling?

A way to deal with errors by anticipating it and handling them.

Signup and view all the flashcards

What is throwing an exception?

The creation of exception with handoff to java runtime system

Signup and view all the flashcards

Study Notes

Exception Handling

  • An event that occurs during program execution which disrupts the normal flow is called an exception
  • When an exception occurs, an exception object is created
  • The exception object will contain information about the situation and the state of the program at that time
  • Throwing an exception refers to the creation of the exception object with the handoff to the Java runtime system

Checked Exceptions

  • Checked exceptions have to be handled to compile
  • These exceptions typically represent common issues the program would be expected to recover from
  • FileNotFoundException is a checked exception
  • The program should not crash if a given file is not found when a FileNotFoundException occurs
  • The program can prompt for a valid file name, or an end program gracefully on a FileNotFoundException
  • EOFException is a checked exception
  • EOFException is an end-of-file exception
  • EOFException is a normal exception when a program is reading a file with an unknown number of values
  • The program can handle this exception and continue on instead of crashing when EOFException occurs
  • The program must address potential Input/Output Exceptions when using an Input/Output stream to compile

Unchecked Exceptions

  • Unchecked exceptions do not have to be addressed to compile
  • Unchecked Exceptions should not occur
  • These exceptions can indicate an error in the logic of the code
  • An example of an unchecked exception is ArrayIndexOutOfBoundsException
  • The compiler is usually unable to anticipate logical errors
  • Programs can use an array without specifying that the method may cause an ArrayIndexOutOfBoundsException
  • The compiler does not force the programmer to account for unchecked exceptions in order to compile

Exceptions vs Errors

  • Exceptions and errors are similar in concept
  • Exceptions and errors are a part of the same hierarchy of objects
  • Exceptions are situations that the program can generally be expected to recover from
  • Errors are situations the program may not be able to recover from

Exception Handling

  • Programs must include a throws clause or a try-catch structure for the exception, if a checked exception can occur during the execution of a program

Try-Catch Structure

  • The general structure begins by declaring a try{}, which contains some Java statements that cause an exception
  • Below the try{}, declare a catch(){}, which contains some Java statements that run if the exception occurs
  • Below the catch(){}, declare finally{}, which contains Java statements that run after the try and catch statements

Execution of a Try Statement

  • The program executes each statement in the try-block
  • If no exception occurs, the program moves to the finally-block and executes the corresponding statements, then the program moves to the next statement to execute after the try statement
  • If an expected exception listed is thrown, the program executes the catch-block, then executes statements listed in the finally-block

FileNotFoundException

  • ReadBinary.java can throw a FileNotFoundException
  • If an IOException occurs, the program terminates abnormally as the IOException is thrown
  • If the 'myData.ser' file is not found, then a FileNotFoundException is thrown
  • Since FileNotFoundException is a child class of IOException the program will terminate and the call trace appears

HandleFileNotFound.java

  • This program's constructor accepts a String
  • This program accepts exception for invalid Social Security number
  • Extends Exception, so the exception is checked
  • getSin() getter for Social Security number

Handling an Exception

  • ReadBinary.java reads only 5 integers
  • The program can be modified to read an arbitrary number of integers
  • If ReadBinary attempts to execute readInt() and there are no more integers to be read, then an EOFException occurs
  • Java docs will specify which exceptions a given method may throw
  • For: https://docs.oracle.com/javase/7/docs/api/java/io/DataInputStream.html#re adInt()
  • readInt() may throw IOException or EOFException

ReadFromBinaryUntilEndOfFile.java

  • Try-catch statement for checking file-not-found exceptions

Handling EndOfFile

  • Reads integers until there are none left
  • Catches the end of the file and IOExceptions and presents a message

Custom Exceptions

  • Exceptions are part of a hierarchy of objects
  • Exception and Error classes are children of the Throwable class
  • RunTimeException class is a child of Exception
  • It is not generally recommended to create new exceptions unless necessary
  • The Throwable class has many subclasses
  • An exception already exists that will meet the programmer's needs

Custom Exceptions Defined

  • A custom exception is a class that extends either Exception or RuntimeException
  • Extend Exception when a programmer needs a checked exception
  • Extend RuntimeException if a programmer requires an unchecked exception

Custom Exception - InvalidSIN

  • Code can throw an exception if an employee has an invalid SIN
  • Programs use the existing InvalidArgumentException, and it is possible to create one
  • INVALID SIN exception would handle above custom exception case

Studying That Suits You

Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

Quiz Team

Related Documents

Description

Learn about exception handling in Java, including checked exceptions like FileNotFoundException and EOFException. Understand how to handle these exceptions to prevent program crashes. Explore strategies for graceful error recovery and user-friendly error messages.

More Like This

Use Quizgecko on...
Browser
Browser