Java Programming in Java Exceptions Handling Errors using Exceptions
Exceptions Exception = Exceptional Event Exceptions are: objects, derived from java.lang.throwable. Throwable Objects: Errors (Java Structure Violation) Exceptions (Java Logic Violations) Runtime Exceptions
Exceptions Creating an Exception Throw an Exception Exception handler appropriate to handle the thrown exception: Catch the Exception Exception Throw Catch
Exception Types Object Throwable Error Exception... RuntimeException......
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.
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.
Call Stack method1 x = method2(...) method2 y = method3(...) method3 z = method4(...)
Error Differentiation Exception Instances of Throwable or subclass of Throwable Object Throwable ArrayException InvalidIndexException NoSuchElementException ElementTypeException
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: Node Class: catch (InvalidIndexException e)... catch (ArrayException e)...
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.
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
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>
Try and Catch: Syntax Diagram Syntax Diagram: try block catch finally (object) block block catch (object) block finally block
Exception Handler Parameter e: exception handler parameter (variable) type of exception, exception message, stack trace. Available methods: e.tostring() e.getmessage() e.printstacktrace()
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(); continue Java
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)
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
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()); Version 1 Output Number is 75 Err: 0-100 expected Java
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();
Writing your own Exception / Example Output Version 1 Number is 75 Class Method Error Messages: Class Class Err1: 0-100 expected Class Class Object Object Err2: SampExcept: 0-100 expected Err3: SampExcept: 0-100 expected at ExTest2.range(ExTest2.java:15) at ExTest2.main(ExTest2.java:28)
I/O-Programmierung catch Java 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();
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);