Exception Handling, File Handling and GUI

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

What listener do we use to get clicks or changes or whatever?

ActionListener

What will happen if you attempt to divide a number by zero in Java?

Attempting to divide a number by zero in Java will result in an ArithmeticException being thrown.

What is the key difference between checked and unchecked exceptions in Java?

Checked exceptions must be either caught or declared in the method signature, while unchecked exceptions do not require explicit handling.

What do you call the pane in the designer file that allows you to see all the components in an organized manner?

Component Tree

Differentiate Error and Exception

Errors are typically thrown by the Java Virtual Machine (JVM) and designate the most serious situations that are unlikely to be recoverable, such as when the system runs out of memory. Exceptions designate situations in which a running program might reasonably be able to recover, for example, when unable to open a data file. Checked exceptions are detected at compile-time, while unchecked exceptions (runtime exceptions) occur due to mistakes in programming logic and are not checked at compile-time.

Is a notification that something interrupts the normal program execution.

Exception

provide a programming paradigm for detecting and reacting to unexpected events.

Exceptions

It is not syntactically correct to have no catch clause when using a try-catch block. (TRUE || FALSE)

FALSE - It is not syntactically correct to have no catch clause when using a try-catch block.

The mistakes in programming logic are checked exceptions

FALSE - The mistakes in programming logic are typically unchecked exceptions, not checked exceptions.

When we open a non-existent file via FileWriter, it will yield an Exception.

FALSE. If the file does not exist, the program will simply create it; it will not raise an exception.

It is good practice to catch the generic Exception class to handle all possible exceptions.

False: Catching the generic Exception class is discouraged because it can lead to catching unintended exceptions and makes it harder to identify and handle specific exceptions.

In Java the IOException is an unchecked exception.

False: IOException is a checked exception in Java. Unchecked exceptions are subclasses of RuntimeException, and IOException does not fall into that category.

In file handling the BufferedReader class is used for writing data to a file.

False: The BufferedReader class in Java is typically used for reading data from a file, not writing to it. For writing, BufferedWriter or FileWriter is commonly used.

The finally block is executed only if an exception is thrown.

False: The finally block is executed whether an exception is thrown or not. It is a block of code that always executes, regardless of whether an exception occurs in the try block or not.

The throws keyword is used in the method body to explicitly throw an exception.

False: The throws keyword is used in the method signature to declare that a method may throw certain exceptions. To actually throw an exception in the method body, the throw keyword is used.

The try block must always be followed by a catch block in exception handling.

False: The try block must be followed by either a catch block or a finally block or both. It is not mandatory to have a catch block if there is a finally block.

The FileNotFoundException is a subclass of IOException.

False: While FileNotFoundException is a subclass of IOException, it is not the only exception that can be thrown when dealing with file operations. Other IO-related exceptions, such as IOException, may also occur.

Which exception is commonly associated with problems in file handling such as a file not being found?

FileNotFoundException is commonly associated with problems in file handling, such as attempting to access a file that does not exist.

Give an example of a checked exception in Java.

IOException is an example of a checked exception in Java, often associated with file I/O operations.

try { System.out.print(13/0); } catch (Exception e) { System.err.print("OOPS "); } catch (ArithmeticException e1) { System.err.print("ILLEGAL "); } finally { System.out.println("Fine"); }

In Java, the catch blocks must be ordered from the most specific exception type to the most general. In the code snippet, you have a catch (Exception e) block before the catch (ArithmeticException e1) block. Since Exception is more general than ArithmeticException, the catch (ArithmeticException e1) block will never be reached, and the code will result in a compilation error.

What class do we extend our code class to?

JFrame

What class allows us to have pop-ups?

JOptionPane

takes care to propagate each exception to the code that can handle it.

Java runtime environment (JRE)

•Exception handling is a mechanism, which allows exceptions to be thrown and caught. This mechanism is provided internally by the

Java runtime environment (JRE)

What component can be able to store other components?

Jpanel

The component that displays text into the frame?

Label

The Exception of parsing a String into an Integer is

NumberFormatException

What button allows us to limit the selection to only one among a group of them?

RadioButton

means extracting data from the file, it is always performed sequentially from the current position of the file.

Reading

In Java, you can create a GUI using?

Swing UI Designer.

The try-catch construct can consists of one or more catch blocks. TRUE OR FALSE

TRUE

It is syntactically correct to have an empty catch clause.

TRUE - It is syntactically correct to have an empty catch clause, although it's generally discouraged because it can hide potential issues.

It is syntactically correct to have multiple catch clauses.

TRUE - It is syntactically correct to have multiple catch clauses.

You can combine different Exception types in one catch clause.

TRUE - You can combine different Exception types in one catch clause, but it's often better practice to have separate catch blocks for each Exception type.

When we open a non-existent file via FileReader, it will yield an Exception.

TRUE. If the file is not found, it will raise a java.io.FileNotFoundException.

It is syntactically correct to have no finally clause. TRUE or FALSE

TRUE. It is syntactically correct to have no finally clause.

What method do we use to put a new line into the file? TRUE or FALSE

TRUE. We use the newLine() method of the BufferedWriter class to put a new line into the file.

What component allows user to enter short texts like name?

TextField

This file is the designer file. It contains an empty window where you can add elements or controls.

The .form file

What does the finally block in exception handling ensure?

The finally block ensures that it is always executed, whether an exception is thrown or not. It is typically used for cleanup operations, such as closing file streams or releasing resources.

In Java what is the superclass of all unchecked exceptions?

The superclass of all unchecked exceptions in Java is RuntimeException.

Which keyword is used in a method signature to declare that the method may throw one or more exceptions?

The throws keyword is used in a method signature to declare that the method may throw one or more exceptions.

means to complete the work with the reader and releases the occupied resources.

To close or disconnect a reader

Contains all the controls that your window can have.

Toolbox

Checked exceptions must be caught or declared in the method signature.

True: Checked exceptions, such as IOException, must be either caught using a catch block or declared in the method signature using the throws clause.

In Java, the FileNotFoundException is a checked exception.

True: FileNotFoundException is a subclass of IOException, which is a checked exception in Java.

It is possible to have nested try-catch blocks in Java.

True: It is possible to have nested try-catch blocks where an inner try-catch block is enclosed within an outer try block.

The close() method in file handling is used to close the file stream and release resources.

True: The close() method in file handling is used to close the file stream and release resources. It is good practice to close the file after reading or writing to avoid resource leaks.

The throws keyword is used in method signatures to indicate that the method may throw an exception.

True: The throws keyword in a method signature indicates that the method may throw one or more exceptions, and it must be handled by the calling method or propagated up the call stack.

Why do we need to close the FileReader and FileWriter?

We need to close the FileReader and FileWriter to release the occupied resources. It is mandatory to close streams after finishing work with them to avoid the risk of damaging the data in the file that has been opened. The finally block is often used to ensure that the close operation is executed even if an exception occurs.

What class do we use to read the file line-by-line?

We use the BufferedReader class to read the file line-by-line.

What class do we use to write into the file in a more efficient manner?

We use the BufferedWriter class to write into the file in a more efficient manner.

What method do we use to read the file line-by-line?

We use the readLine() method of the BufferedReader class to read the file line-by-line.

means sending data to the file in a specific way, it is performed from the current position of the file.

Writing

Is it mandatory to catch or declare checked exceptions in Java?

Yes, it is mandatory to catch or declare checked exceptions in Java.

When an exception arises, the state of the program is saved, the normal flow is interrupted, and the control is passed to an?

exception handler

When you use write(), it is essential to follow it with a call to ANSWER() to ensure that any buffered data is actually written to the file.

flush()

is a method used to flush the stream. It forces any buffered output bytes to be written out. In the context of file I/O, calling this is important to ensure that any buffered data is actually written to the file rather than being held in memory.

flush()

To get whether a radio button is selected, What is this method that returns a Boolean on whether it is selected or not.

isSelected()

When we open a non-existent file via FileReader, it will yield an Exception, what is the name of the Exception that it will throw?

java.io.FileNotFoundException.

is a method in the BufferedWriter class that writes a line separator. It is a platform-independent way to insert a space character or characters. This is useful for creating text files that have lines separated by the appropriate line separator for the operating system.

newLine()

Allows you to specify the behavior of the program when you close the JFrame.

setDefaultCloseOperation

Allows the JFrame to occupy space in the screen.

setSize

This method in main allows your JFrame to be finally visible

setVisible

Explain the role of the finally block in Java exception handling, particularly in the context of working with external resources. Why is it considered good practice to place resource cleanup operations, such as closing files or network connections, inside a finally block rather than relying solely on try-catch blocks?

the finally block in Java is a powerful mechanism for ensuring that cleanup operations, especially those related to external resources, are consistently performed. This helps in maintaining code reliability, preventing resource leaks, and adhering to best practices in exception handling and resource management.

Exceptions in Java are thrown using the keyword

throw

The keyword that allows us to give Exceptions is

throw

The keyword put into a method signature where it is possible for it to yield Exceptions is

throws

The keyword for the block of codes that may yield an Exception that we want to be caught is

try-catch

In catching exceptions, we must surround the code that could throw an exception with?

try-catch block

If there are a lot of options

which component do we use instead?,ComboBox

FileReader vs. BufferedReader

while both classes are used for reading files, FileReader is a basic class for reading characters from a file, and BufferedReader is a more advanced class that adds buffering for improved efficiency and provides additional methods for reading lines and arrays of characters.

is a method used in Java for writing data to a file or an output stream. It is commonly associated with classes like FileWriter or BufferedWriter.

write()


Ensembles d'études connexes

BIO EXAM 1 (questions got correct)

View Set

MKTG 4030: Chapter 9, MKTG 4030: Chapter 10, MKTG 4030: Chapter 13

View Set

Mental Health Varcarolis Ch. 9: Legal and Ethical Issues - PrepUs

View Set

BMF 2: L. 8 Genes, Chromosomes and DNA Packaging

View Set

Factoring Polynomial Expressions Unit Test 88%

View Set

Manifestation of the Lifeblood Theory

View Set