Programming in Java Exceptions Handling Errors using Exceptions

Transcription

Programming in Java Exceptions Handling Errors using Exceptions
Fachbereich Informatik und Elektrotechnik
Java
Programming in Java
Exceptions
Handling Errors using Exceptions
Programming in Java,  Helmut Dispert
Fachbereich Informatik und Elektrotechnik
Exceptions
Exception = Exceptional Event
Exceptions are:
objects,
derived from java.lang.Throwable.
Throwable Objects:
Errors
(Java Structure Violation)
Exceptions
(Java Logic Violations)
Runtime Exceptions
Programming in Java,  Helmut Dispert
Fachbereich Informatik und Elektrotechnik
Exceptions
Creating an Exception
Throw an Exception
Exception handler appropriate to handle the
thrown exception:
Catch the Exception
Exception
Throw
Programming in Java,  Helmut Dispert
Catch
Fachbereich Informatik und Elektrotechnik
Exception Types
Object
Throwable
Exception
Error
RuntimeException
...
...
Programming in Java,  Helmut Dispert
...
Fachbereich Informatik und Elektrotechnik
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.
Programming in Java,  Helmut Dispert
Fachbereich Informatik und Elektrotechnik
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.
Programming in Java,  Helmut Dispert
Fachbereich Informatik und Elektrotechnik
Call Stack
method1
{
x = method2(...)
}
method2
{
y = method3(...)
}
method3
{
z = method4(...)
}
Programming in Java,  Helmut Dispert
Fachbereich Informatik und Elektrotechnik
Error Differentiation
Exception
⇒
Instances of Throwable
or subclass of Throwable
Object
Throwable
ArrayException
InvalidIndexException
NoSuchElementException
ElementTypeException
Programming in Java,  Helmut Dispert
Fachbereich Informatik und Elektrotechnik
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)
{
...
}
Programming in Java,  Helmut Dispert
Fachbereich Informatik und Elektrotechnik
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.
Programming in Java,  Helmut Dispert
Fachbereich Informatik und Elektrotechnik
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”
Programming in Java,  Helmut Dispert
Fachbereich Informatik und Elektrotechnik
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>
}
Programming in Java,  Helmut Dispert
Fachbereich Informatik und Elektrotechnik
Try and Catch: Syntax Diagram
Syntax Diagram:
try
block
Programming in Java,  Helmut Dispert
catch
(object)
finally
block
catch
(object)
block
block
finally
block
Fachbereich Informatik und Elektrotechnik
Exception Handler Parameter
e: exception handler parameter (variable)
type of exception,
exception message,
stack trace.
Available methods:
e.toString()
e.getMessage()
e.printStackTrace()
Programming in Java,  Helmut Dispert
Fachbereich Informatik und Elektrotechnik
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
Programming in Java,  Helmut Dispert
Fachbereich Informatik und Elektrotechnik
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)
Programming in Java,  Helmut Dispert
Fachbereich Informatik und Elektrotechnik
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
Programming in Java,  Helmut Dispert
Fachbereich Informatik und Elektrotechnik
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
Programming in Java,  Helmut Dispert
Fachbereich Informatik und Elektrotechnik
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();
}
}
}
Programming in Java,  Helmut Dispert
Fachbereich Informatik und Elektrotechnik
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)
Programming in Java,  Helmut Dispert
Object
Fachbereich Informatik und Elektrotechnik
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();
}
}
}
Programming in Java,  Helmut Dispert
Java
Fachbereich Informatik und Elektrotechnik
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);
}
}
Programming in Java,  Helmut Dispert

Documents pareils