Download Exception - Websupport1

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
Exceptions

Three categories of errors:






Syntax errors
Runtime errors
Logic errors
Syntax errors: rules of the language have not been
followed.
Runtime error: program is running and
environment detects an operation that is
impossible to carry out.
Logic errors: the program doesn’t perform the
way it intend to
Runtime error

Runtime errors occur for various reason








Invalid input
Open a file which doesn’t exist
Access out-of-bounds array
…
Runtime errors cause exceptions
Exception are handled differently from the events of GUI
programming
A event may be ignored in GUI programming, but an
exception cannot be ignored.
Java provides programmers with the capability to
elegantly handle runtime errors.
An example
import javax.swing.JOptionPAne;
public class test{
public static void main(String[] args){
String input =
JOptionPane.showInputDialog(null,
“Please enter an integer”);
int number = Integer.parseInt(input);
JOptionPane.shoeMessageDialog(null, “the
number entered is “ + number);
}
} // if you entered a floating-point
value, the program terminates
Catch this error: try–catch block
import javax.swing.JOptionPAne;
public class test{
public static void main(String[] args){
try{
String input = JOptionPane.showInputDialog (null, “Please
enter an integer”);
int number = Integer.parseInt(input);
JOptionPane.shoeMessageDialog(null, “the number entered is
“ + number);
}
catch{ Exception ex)
{ JOptionPane.showMessageDialog(null, “Incorrect input: an
integer is required”);
}
}
}
Exception classes






A Java exception is an instance of a class derived from
Throwable.
The Throwable class is contained in the java.lang package
Subclasses of Throwable are contained in various
packages
Errors related to GUI components are included in the
java.awt package;
Numeric exceptions are included in the java.lang package
You can create your own exception classes by extending
Throwable or a subclass of Throwable.
Exception classes

The exception classes can be classified into three types:
 System errors: are thrown by the JVM and
represented in the Error class. The Error class
describe internal system errors
 Exceptions: are represented in the Exception class,
which describe errors caused by your program and by
external circumstance
 Runtime exceptions; are represented in the
RuntimeException class, which describes
programming errors, such as bad casting, accessing
an out-of –bounds array, and numeric errors.
Exception classes and subclasses

Object

Throwable

Exception





ClassNotFoundException
IOException
AWTException
RuntimeException
 ArithmeticException
 NullPointerException
 IndexOutOfBoundsException
Error




LinkageError
VirtualMachineError
AWTError
…
Unchecked Exceptions







RuntimeException, Error and their subclasses
Reflect programming logic errors
Not recoverable
Should be corrected in the program
Can occur anywhere
Java does not mandate that you write code to
catch or declare
For example, a NullPointerException is thrown if
you access an object through a reference variable
before an object is assigned to it;
Checked exception


All other exception are known as checked
exception
The compiler forced the programmer to
check and deal with them
Exception handling

Java’s exception-handling model is based on
three operations





Declaring an exception
Throwing an exception
Catching an exception
Every method must state the types of checked
exceptions it might throw
Java doesnot require that you declare Error and
RuntimeException.
Declaring exception









Other exception thrown by the method must be explicitly
declared
public void myMethod() throws IOException
{
…
}
If the method throws multiple exceptions
public void myMehtod() throws Exception1, Exception2,
…, ExceptionN
{
}
Throwing Exceptions





Suppose the argument must be non-negative, but
a negative argument is passed, the program can
created an instance of IllegalArgumentException
and throw it
IllegalArgumentException ex = new
IllegalArgumentException(“Wrong Argument”);
throw ex;
Or
throw new IllegalArgumentException(“Wrong
Argument”);
Catching Exceptions











try{
statements;
}
catch (Exception1 e1)
{
}
catch (Exception2 e2)
{
}
finally{
}