Download Lecture 8 A

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Lecture 8
CS 202
Errors
Three types of errors
• Syntax/Compile time
• Runtime
• Logic
Errors
Syntax/Compiletime Errors
– Incorrect Java, caught by IDE or compiler
int x == 0;
Errors
Runtime errors occur when an operation is
impossible to carry out. Without additional
handling, they cause the application to crash:
• Read from a file that does not exist
• Integer.parseInt(1.2);
public static void main(String[] args){
int quotient = 0;
for(int counter = 10; counter >=0; counter--){
System.out.print("10 / " + counter + " = ");
quotient = 10/counter;
System.out.println (quotient);
}
}
Errors
Logic Errors
• Logical problems in the application
• Cause incorrect results, not crashes
String[] options = {"Yes", "No"};
int buy = 0;
JOptionPane.showOptionDialog(null, "Place Order?", "Buy Dialog",
JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE, null,
options, "Yes");
if(x ==0);
buyProduct();
Stack Trace
• A stack trace is a list of method calls and code line numbers,
usually seen when an error occurs:
• You should love a stack trace like a mother who only criticizes
you for your own good.
• Note that you can tell what line of your code was executing
when the error occurred, but you may have to read several
lines of the trace to get to it.
• Generally, you should read down the trace until you see a line
of code that you wrote, and look there for the problem.
Exceptions
Definitions of "exception:"
• Wikipedia:
"An anomalous event requiring special processing"
• Liang Introduction To Java Programming, referring to Java
exceptions specifically:
"An object that represents an error or condition that prevents
execution from proceeding normally."
• In Java, an exception is an object!
Exceptions
• Various kinds of Exceptions are defined in different Exception
classes. Many of them are recoverable, meaning that it is possible
to write code that prevents the application from terminating when
they occur.
• Exception classes have methods; the ones you are likely to use
involve stack traces and error messages.
• Exceptions are "raised" or "thrown" by code, either written by you
or by the author of a class you are using.
Exceptions:
• You can create your own exceptions to deal with the problems that are likely to
arise in your programs, but for now, use the ones that are built in to the Java
API. You have already encountered many of these
• This code will raise a StackOverflowException :
public class Demo {
int x;
public Demo(int oldX){
x = oldX + 1;
System.out.println(x);
Demo d = new Demo(x);
}
public static void main(String[] args) {
Demo demo = new Demo(1);
}
}
Exceptions
Try/Catch:
• Run an exception-prone operation in try{}; if an
exception occurs, deal with it in catch{}
• Optional finally{} runs after the try code, whether or
not an exception occurs
• We have already seen this in the file parsing material
Try…catch…finally
Try-Catch-Finally
try{
error-prone code
}
catch(TypeOfException nameForException){
what to do if this type of exception occurs
}
catch(OtherTypeOfException nameForException){
what to do if this type of exception occurs
}
catch(Exception ex){
what to do if any other type of exception occurs
}
…
finally{
code to execute afterwards, whether or not an exception occurred
}
Try-Catch-Finally
• Consider this error-prone code:
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter two numbers:");
double num1 = input.nextDouble();
double num2 = input.nextDouble();
double result = num1 / num2;
System.out.println(num1 + "/" + num2 + "=" + result);
input.close();
}
Try-Catch-Finally
public static double quotient(double num1, double num2) {
if (num2 == 0)
throw new ArithmeticException("Divisor cannot be zero");
return num1 / num2;
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter two numbers:");
try {
double num1 = input.nextDouble();
double num2 = input.nextDouble();
double result = quotient(num1, num2);
System.out.println(num1 + "/" + num2 + "=" + result);
}
catch (ArithmeticException ex) {
System.out.println(ex.getMessage());
} finally {
input.close();
}
System.out.println("festivities continue without a crash!");
}
Throwing Exceptions
• The exceptions we have seen so far are thrown by code you did
not write
• You can also check for exceptional conditions and throw
exceptions, either standard ones or your own
• See the textbook for syntax for creating your own exception
types. Only do this when none of the standard ones seem
correct.
Checked Exceptions
• The compiler will force you to handle certain kinds of exceptions
that might arise; these are called Checked Exceptions.
• Recall the file operations in week 2. Since file I/O is so errorprone, many file operations throw exceptions which you must
catch in order to use the File, PrintWriter, etc. classes.