Chapter 12: Exception Handling and Text I/O Flashcards

1
Q

What are exceptions?

A

A representation of something that would be a run-time error or condition that prevents the program from running as intended.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Advantages of exceptions?

A

It enables a method to throw an exception to its caller, enabling the caller to handle the exception.

The key benefit of exception handling is separating the detection of an error (done in a called method) from the handling of an error (done in the calling method).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What’s an error vs an exception

A

System errors and runtime errors are usually unrecoverable and indicate there is code in your program than should be corrected.
Exceptions can be caught and handled within the program, try not to overuse the try-catch blocks.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How do you declare exceptions in methods and throw exceptions to methods?

A

All exceptions that could be thrown by the method must be declared in the header. To declare an exception, you use the THROWS keyword. Use commas to separate exceptions if there are multiple.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

When do you use a try-catch block?

A

Use a try-catch block when you’re setting up a possible exception that isn’t going to be handled by something like an if statement. Use it when having to deal with unexpected error conditions.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How do you set up a try-catch block?

A

The try keyword should be used first, and each catch block should have its own unique exception to catch following the try block.

try {
  statements; // Statements that may throw exceptions
}
catch (Exception1 exVar1) {
  handler for exception1;
}
catch (Exception2 exVar2) {
  handler for exception2;
}
...
catch (ExceptionN exVarN) {
  handler for exceptionN;
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Explain the finally clause.

A

The finally clause is a tool in Java that allows you to have a section of code executed whether an exception is caught or not.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

When should you use an exception?

A

Simple errors should be tried to fix with a simpler method such as if statements.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Syntax for re-throwing an exception for you to look at.

A
try {
  statements;
}
catch (TheException ex) {
  perform operations before exits;
  throw ex;
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is a chained exception?

A

When you throw multiple exceptions with the later ones possibly having new information.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How do you define your own custom exception?

A

Use an extension from the java.lang.Exception class.

 1  public class InvalidRadiusException extends Exception {
 2    private double radius;
 3 
 4    /** Construct an exception */
 5    public InvalidRadiusException(double radius) {
 6      super("Invalid radius " + radius);
 7      this.radius = radius;
 8    }
 9 
10    /** Return the radius */
11    public double getRadius() {
12      return radius;
13    }
14  }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is the File class used for?

A

To permanently store data that is created/stored when a program runs.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is a relative file name and an absolute file name?

A

The name of the current file itself not the full directory of it.

Welcome.java — is a relative file name

c:\book\Welcome.java. — is an absolute file name

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is the PrintWritter class used for?

A

Creating a file and writing data into a text file.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is the PrintWriter class used for?

A

Creating a file and writing data into a text file.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is the scanner class used for?

A

It is used to collect user inputs.