Chapter 12: Exception Handling and Text I/O Flashcards
What are exceptions?
A representation of something that would be a run-time error or condition that prevents the program from running as intended.
Advantages of exceptions?
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).
What’s an error vs an exception
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 do you declare exceptions in methods and throw exceptions to methods?
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.
When do you use a try-catch block?
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 do you set up a try-catch block?
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; }
Explain the finally clause.
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.
When should you use an exception?
Simple errors should be tried to fix with a simpler method such as if statements.
Syntax for re-throwing an exception for you to look at.
try { statements; } catch (TheException ex) { perform operations before exits; throw ex; }
What is a chained exception?
When you throw multiple exceptions with the later ones possibly having new information.
How do you define your own custom exception?
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 }
What is the File class used for?
To permanently store data that is created/stored when a program runs.
What is a relative file name and an absolute file name?
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
What is the PrintWritter class used for?
Creating a file and writing data into a text file.
What is the PrintWriter class used for?
Creating a file and writing data into a text file.