Download Programming in Java Exceptions Handling Errors using Exceptions

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
China Jiliang University
Java
Programming in Java
Exceptions
Handling Errors using Exceptions
Java Web Applications,  Helmut Dispert
China Jiliang University
Exceptions
Exception = Exceptional Event
Exceptions are:
Ø objects,
Ø derived from java.lang.Throwable.
Throwable Objects:
(Java Structure Violation)
Ø Errors
(Java Logic Violations)
Ø Exceptions
Ø Runtime Exceptions
Java Web Applications,  Helmut Dispert
China Jiliang University
Exceptions
Creating an Exception
Throw an Exception
Exception handler appropriate to handle the
thrown exception:
Catch the Exception
Exception
Throw
Java Web Applications,  Helmut Dispert
Catch
China Jiliang University
Exception Types
Object
Throwable
Error
Exception
RuntimeException
...
...
Java Web Applications,  Helmut Dispert
...
China Jiliang University
Exception Types
Errors:
Dynamic Linking Error, hard failure in the virtual machine
Ø VM throws an Error.
Exceptions:
Indication of a not very serious systematic problem
Ø Program throws an Exception.
Runtime Exceptions:
Exceptions that occur within the Java virtual machine
during runtime
(e.g. NullPointerException).
Ø VM throws a Runtime Exception.
Java Web Applications,  Helmut Dispert
China Jiliang University
Exception Types
Advantages of Java's Error Handling
1. Error Handling Code is separated from regular code.
2. Error Propagation up the Call Stack.
3. Grouping Error Types and Error Differentiation.
Java Web Applications,  Helmut Dispert
China Jiliang University
Call Stack
method1
{
x = method2(...)
}
method2
{
y = method3(...)
}
method3
{
z = method4(...)
}
Java Web Applications,  Helmut Dispert
China Jiliang University
Error Differentiation
Exception
⇒
Instances of Throwable
or subclass of Throwable
Object
Throwable
ArrayException
InvalidIndexException
NoSuchElementException
ElementTypeException
Java Web Applications,  Helmut Dispert
China Jiliang University
Error Differentiation
Leaf Class (class without subclass):
Ø represents a specific type of exception,
Ø specialized handler.
Node Class (class with one or more subclass):
Ø represents a group of related exceptions,
Ø general handler.
Leaf Class:
catch (InvalidIndexException e)
{
...
}
Node Class:
catch (ArrayException e)
{
...
}
Java Web Applications,  Helmut Dispert
China Jiliang University
Catch or Specify Requirement
Ø Catch
Ø Specify Checked Exception
Exception Types:
Ø Runtime Exceptions
Ø I/O-Exceptions
Ø Own Exceptions
A method either has to catch or specify all checked exceptions
that can be thrown within it's scope.
Runtime Exceptions:
Don't have to be caught or specified.
Checked Exceptions:
Not runtime exceptions, checked by the compiler;
checked exceptions must be caught or specified.
Java Web Applications,  Helmut Dispert
China Jiliang University
Catch or Specify Requirement
Checked Exceptions:
Ø Due to external circumstances that the programmer cannot
prevent
Ø Compiler checks that these exceptions are handled
Ø All IOExceptions are checked exceptions
Unchecked Exceptions:
Ø Programmers fault, can be prevented
Ø Included exceptions:
e.g. NullPointerException, ArithmeticException
Ø e.g. to avoid the NullPointerException the reference can be
checked for “null”
Java Web Applications,  Helmut Dispert
China Jiliang University
Try and Catch
Critical statements are capsuled in
try-catch statements
try
{
<some dangerous action>
}
catch (Exception e)
{
<some reasonable behaviour>
}
.
.
.
finally
{
<something absolutely necessary>
}
Java Web Applications,  Helmut Dispert
China Jiliang University
Try and Catch: Syntax Diagram
Syntax Diagram:
try
block
catch
(object)
finally
block
catch
(object)
Java Web Applications,  Helmut Dispert
block
block
finally
block
China Jiliang University
Exception Handler Parameter
e: exception handler parameter (variable)
Ø type of exception,
Ø exception message,
Ø stack trace.
Available methods:
Ø e.toString()
Ø e.getMessage()
Ø e.printStackTrace()
Java Web Applications,  Helmut Dispert
China Jiliang University
Try and Catch
Example: HelloWorld with Exception Handling
// Hello program with exception handling
class ExceptionalHello2
{
public static void main (String args[])
{
try
/* Now let's say hello */
{
System.out.println("Hello " + args[0]); // line 9
}
catch (Exception e)
{
System.out.println("Hello! Who are you?!");
// e.printStackTrace();
}
}
}
Java
continue
Java Web Applications,  Helmut Dispert
China Jiliang University
Try and Catch
continued
java ExceptionalHello2
Output:
Hello! Who are you?!
with (inside catch block):
e.printStackTrace();
Output:
Hello! Who are you?!
java.lang.ArrayIndexOutOfBoundsException:
at
ExceptionalHello2.main(ExceptionalHello2.java:9)
Java Web Applications,  Helmut Dispert
China Jiliang University
Writing your own Exception / Example
class SampExcept extends Exception
{
SampExcept (String Message) // Constructor
{
super(Message);
// pass message to parent
}
}
public class ExTest
{
static void range(int numb) throws SampExcept
{
if(numb < 0 || numb > 100)
{
throw new SampExcept("0-100 expected"); // line 15
}
else
{
System.out.println("Number is " + numb);
}
}
continue
Java Web Applications,  Helmut Dispert
China Jiliang University
Writing your own Exception / Example
Version 1
public static void main (String args[])
{
try
{
range(75);
range(250);
// line 28
}
catch(SampExcept e)
{
System.out.println("Err: " + e.getMessage());
}
}
}
Output
Number is 75
Err: 0-100 expected
Java
Java Web Applications,  Helmut Dispert
China Jiliang University
Writing your own Exception / Example
Version 2
public static void main (String args[])
{
try
{
range(75);
range(250);
// line 28
}
catch(SampExcept e)
{
System.out.println("\n\nError Messages:");
System.out.println("\nErr1:\n" + e.getMessage());
System.out.println("\nErr2:\n" + e.toString());
System.out.println("\nErr3: " );
e.printStackTrace();
}
}
}
Java Web Applications,  Helmut Dispert
China Jiliang University
Writing your own Exception / Example
Output
Version 1
Class
Number is 75
Error Messages:
Err1:
0-100 expected
Method
Class
Class
Class
Class
Object
Err2:
SampExcept: 0-100 expected
Err3:
SampExcept: 0-100 expected
at ExTest2.range(ExTest2.java:15)
at ExTest2.main(ExTest2.java:28)
Java Web Applications,  Helmut Dispert
Object
China Jiliang University
I/O-Programmierung
catch
import java.io.*;
public class IOTest2
{
public static void main(String[] args)
{
try
{
byte bArray[] = new byte[128];
System.out.print("Texteingabe: ");
System.in.read(bArray);
String s = new String(bArray,0);
System.out.print("Eingabe: ");
System.out.println(s);
}
catch(IOException ioe)
{
System.out.println(ioe.toString());
ioe.printStackTrace();
}
}
}
Java Web Applications,  Helmut Dispert
Java
China Jiliang University
I/O-Programmierung
specify
Java
import java.io.*;
public class IOTest22
{
public static void main(String[] args) throws IOException
{
byte bArray[] = new byte[128];
System.out.print("Texteingabe: ");
System.in.read(bArray);
String s = new String(bArray,0);
System.out.print("Eingabe: ");
System.out.println(s);
}
}
Java Web Applications,  Helmut Dispert
Related documents