Programming in Java Exceptions Handling Errors using...
Transcript of Programming in Java Exceptions Handling Errors using...
Fachbereich Informatik und Elektrotechnik
Programming in Java, Helmut Dispert
Java
Programming in Java
Exceptions
Handling Errors using Exceptions
Fachbereich Informatik und Elektrotechnik
Programming in Java, Helmut Dispert
Exceptions
Exception = Exceptional Event
Exceptions are:
� objects,
� derived from java.lang.Throwable.
Throwable Objects:
� Errors (Java Structure Violation)
� Exceptions (Java Logic Violations)
� Runtime Exceptions
Fachbereich Informatik und Elektrotechnik
Programming in Java, Helmut Dispert
Exceptions
Throw Catch
Exception
Creating an Exception
Throw an Exception
Exception handler appropriate to handle the
thrown exception:
Catch the Exception
Fachbereich Informatik und Elektrotechnik
Programming in Java, Helmut Dispert
Exception Types
Object
Throwable
Exception
RuntimeException
Error
...
... ...
Fachbereich Informatik und Elektrotechnik
Programming in Java, Helmut Dispert
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.
Fachbereich Informatik und Elektrotechnik
Programming in Java, Helmut Dispert
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.
Fachbereich Informatik und Elektrotechnik
Programming in Java, Helmut Dispert
Call Stack
method1
{
x = method2(...)
}
method2
{
y = method3(...)
}
method3
{
z = method4(...)
}
Fachbereich Informatik und Elektrotechnik
Programming in Java, Helmut Dispert
Error Differentiation
Exception ⇒⇒⇒⇒ Instances of Throwable
or subclass of Throwable
Object
Throwable
ArrayException
InvalidIndexException NoSuchElementException
ElementTypeException
Fachbereich Informatik und Elektrotechnik
Programming in Java, Helmut Dispert
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){
...}
Fachbereich Informatik und Elektrotechnik
Programming in Java, Helmut Dispert
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.
Fachbereich Informatik und Elektrotechnik
Programming in Java, Helmut Dispert
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”
Fachbereich Informatik und Elektrotechnik
Programming in Java, Helmut Dispert
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>
}
Fachbereich Informatik und Elektrotechnik
Programming in Java, Helmut Dispert
Try and Catch: Syntax Diagram
Syntax Diagram:
try block catch
finally block
(object) block
catch (object) block finally block
Fachbereich Informatik und Elektrotechnik
Programming in Java, Helmut Dispert
Exception Handler Parameter
e: exception handler parameter (variable)
� type of exception,
� exception message,
� stack trace.
Available methods:
� e.toString()
� e.getMessage()
� e.printStackTrace()
Fachbereich Informatik und Elektrotechnik
Programming in Java, Helmut Dispert
Try and Catch
// 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();
}
}
}
Example: HelloWorld with Exception Handling
continue
Java
Fachbereich Informatik und Elektrotechnik
Programming in Java, Helmut Dispert
Try and Catch
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)
continued
Fachbereich Informatik und Elektrotechnik
Programming in Java, Helmut Dispert
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
Fachbereich Informatik und Elektrotechnik
Programming in Java, Helmut Dispert
Writing your own Exception / Example
public static void main (String args[])
{
try
{
range(75);
range(250); // line 28
}
catch(SampExcept e)
{
System.out.println("Err: " + e.getMessage());
}
}
}
Number is 75
Err: 0-100 expected
Version 1
Output
Java
Fachbereich Informatik und Elektrotechnik
Programming in Java, Helmut Dispert
Writing your own Exception / Example
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();
}
}
}
Version 2
Fachbereich Informatik und Elektrotechnik
Programming in Java, Helmut Dispert
Writing your own Exception / Example
Number is 75
Error Messages:
Err1:
0-100 expected
Err2:
SampExcept: 0-100 expected
Err3:
SampExcept: 0-100 expected
at ExTest2.range(ExTest2.java:15)
at ExTest2.main(ExTest2.java:28)
Version 1Output
Class Class
Class Class
Class
Method
Object Object
Fachbereich Informatik und Elektrotechnik
Programming in Java, Helmut Dispert
I/O-Programmierung
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();
}}
}
catchJava
Fachbereich Informatik und Elektrotechnik
Programming in Java, Helmut Dispert
I/O-Programmierung
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);
}
}
specifyJava