Podcast
Questions and Answers
What is the primary characteristic of an exception in programming?
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.
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?
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.
______ exceptions are those that must be handled in order for the program to compile.
Which of the following is an advantage of handling FileNotFoundException
?
Which of the following is an advantage of handling FileNotFoundException
?
Unchecked exceptions must be addressed in order to compile.
Unchecked exceptions must be addressed in order to compile.
What is the primary reason the compiler does not force the handling of unchecked exceptions?
What is the primary reason the compiler does not force the handling of unchecked exceptions?
An ArrayIndexOutOfBoundsException
is an example of an ______ exception.
An ArrayIndexOutOfBoundsException
is an example of an ______ exception.
What is the key difference between exceptions and errors in Java?
What is the key difference between exceptions and errors in Java?
Exceptions and errors are unrelated concepts and do not share a common inheritance hierarchy
Exceptions and errors are unrelated concepts and do not share a common inheritance hierarchy
What two actions must a program take when an checked exception could occur?
What two actions must a program take when an checked exception could occur?
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.
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.
Match each code block with its function in exception handling:
Match each code block with its function in exception handling:
In the context of the ReadBinary.java
example, what happens when an IOException
occurs and is not caught within a try-catch
block?
In the context of the ReadBinary.java
example, what happens when an IOException
occurs and is not caught within a try-catch
block?
Declaring throws IOException
in a method signature means the method must handle the exception internally.
Declaring throws IOException
in a method signature means the method must handle the exception internally.
In the HandleFileNotFound.java
code, what output is printed to the console if the specified file is successfully found?
In the HandleFileNotFound.java
code, what output is printed to the console if the specified file is successfully found?
In the HandleFileNotFound.java
example, the DataInputStream
is initialized to ______ before the try
block.
In the HandleFileNotFound.java
example, the DataInputStream
is initialized to ______ before the try
block.
What exception might readInt()
throw?
What exception might readInt()
throw?
It is generally recommended to create custom exceptions to handle specific application-level problems.
It is generally recommended to create custom exceptions to handle specific application-level problems.
If a custom checked exception is required, what class should the custom exception extend?
If a custom checked exception is required, what class should the custom exception extend?
Flashcards
What is an exception?
What is an exception?
An event during program execution that disrupts the normal flow.
What are checked exceptions?
What are checked exceptions?
Exceptions that must be handled in order for the code to compile.
What are unchecked exceptions?
What are unchecked exceptions?
Exceptions that do not need to be addressed in order to compile.
What is a try-catch?
What is a try-catch?
Signup and view all the flashcards
What is 'finally' block?
What is 'finally' block?
Signup and view all the flashcards
What is custom exception?
What is custom exception?
Signup and view all the flashcards
What is Exception Handling?
What is Exception Handling?
Signup and view all the flashcards
What is throwing an exception?
What is throwing an exception?
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 acatch(){}
, which contains some Java statements that run if the exception occurs - Below the
catch(){}
, declarefinally{}
, 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.
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.