Module 8 Exception
Module 8 Exception
(Java)
Module 8
Exception
Familiarize themselves with error and exception
handling code
8.1
Exception
EXCEPTION
Exception
CONCEPTS OF EXCEPTION
Exception
CONCEPTS OF EXCEPTION
Exception
The call stack
https://docs.oracle.com/javase/tutorial/essential/exceptions/definition.html
Exception
CONCEPTS OF EXCEPTION
Exception
Searching the call stack for the exception handler
https://docs.oracle.com/javase/tutorial/essential/exceptions/definition.html
Exception
Exception Class Hierarchy
Object
Throwable
Exception Error
Runtime Exception
Exception
Exception-Handling Keywords
Øtry
Øcatch
Øthrow
Øthrows
Øfinally
Exception
Exception-Handling Techniques
Exception
Exception-Handling Techniques
Exception
Exception-Handling Techniques
Exception
The try / catch and finally Blocks
try{
//codes that may throw exception
}
catch(ExceptionType e1){
//codes that handle exception e1
}
catch(ExceptionType e2){
//codes that handle exception e2
}
finally{
/*codes to execute whether or not errors are
encountered*/
}
Exception
Exception
Exceptions thrown during execution of the try
block can be caught and handled in a
catch block.
The code in the finally block is always executed.
Exception
Exception
Exception
Exception
Exception
Exception
Exception
The throw and throws Statements
public void methodName throws ExceptionList
{
//statements
throw ThrowableInstance;
}
where:
ExceptionList – list of exception types separated by
comma
ThrowableInstance – an instance (object) of an
exception
Exception
Common Predefined Exceptions
Exception
Common Predefined Exceptions
Exception
Common Predefined Exceptions
ØArrayIndexOutOfBoundsException – is
thrown when an attempt is made to access
an element in the array that is beyond the
index of the array.
Exception
User-defined Exceptions
Steps on how to create and use customized
exceptions:
Exception
Example
Exception
Throwing Exceptions (throw)
class ThrowDemo {
public static void main(String args[]){
String input = “invalid input”;
try {
if (input.equals(“invalid input”)) {
throw new RuntimeException("throw demo");
} else {
System.out.println(input);
}
System.out.println("After throwing");
} catch (RuntimeException e) {
System.out.println("Exception caught here.");
System.out.println(e);
}
}
}
Exception
Throwing Exceptions (throws)
public class ThrowingClass {
static void myMethod() throws ClassNotFoundException {
throw new ClassNotFoundException ("just a demo");
}
}
class ThrowsDemo {
public static void main(String args[]) {
try {
ThrowingClass.myMethod();
} catch (ClassNotFoundException e) {
System.out.println(e);
}
}
}
Exception
Creating your own Exception
class HateStringException extends RuntimeException{
/* No longer add any member or constructor */
}
class TestHateString {
public static void main(String args[ ]) {
String input = "invalid input";
try {
if (input.equals("invalid input")) {
throw new HateStringException();
}
System.out.println("String accepted.");
} catch (HateStringException e) {
System.out.println("I hate this string: " + input +
".");
}}}
Exception
Assertions
8.2
Exception
ASSERTION
Exception
Ø By verifying that the boolean expression is
indeed true, the assertion confirms your
assumptions about the behavior of your
program, increasing your confidence that
the program is free of errors.
Exception
Types of Assertion:
Exception
The assertion statement has two forms:
Exception
ASSERTION
Enabling assertions onNetBeans
To enable runtime assertion checking in NetBeans
IDE, simply follow these steps:
1. Look for the project name in the Projects panel on
the left. Right click on the
project name or icon.
2. Select Properties. This is the last option of the
drop-down menu.
3. Click on Run from the Categories panel on the left.
4. Enter -ea for VM Options text field.
5. Click on OK button.
6. Compile and run the program as usual.
Exception
ASSERTION
Here is an example that uses the simple form of
assert.
class AgeAssert {
public static void main(String args[]) {
int age = 0;
assert(age>0);
/* if age is valid (i.e., age>0) */
if (age >= 18) {
System.out.println(“Congrats! You're an adult!
=)”);
}
}
}
Exception
ASSERTION
Here is an example that uses the simple form of
assert.
class AgeAssert {
public static void main(String args[]) {
int age = 0;
assert(age>0) : "age should at least be 1";
/* if age is valid (i.e., age>0) */
if (age >= 18) {
System.out.println(“Congrats! You're an adult!
=)”);
}
}
}
Exception
Complex Assertion Form
Exception
Complex Assertion Form
Exception
When an Assertion Fails
Exception
When To Use Assertions
ØInternal Invariants
Many programmers use comments to
indicate programming assumptions.
if (i % 3 == 0) { ... }
else if (i % 3 == 1) { ... }
else { // We know (i % 3 == 2)
... }
Exception
When To Use Assertions
if (i % 3 == 0) { ... }
else if (i % 3 == 1) { ... }
else { assert i % 3 == 2 : i; ... }
Exception
When To Use Assertions
ØControl Flow
If a program should never reach a point,
then a
constant false assertion may be used.
void foo() {
for (...) {
if (...)
return;
}
assert false; // Execution
should never get here
}
Exception
Example: AssertionExample.java
import java.util.*;
import java.util.Scanner;
Exception
To run the example:
Exception
Backiel, A. (2015). Beginning Java Programming: The Object-oriented Approach. 1st Edition
Fain, Y. (2015). Java programming 24-hour trainer. John Wiley and Sons:Indianapolis, IN
Smith, J. (2015). Java programs to accompany Programming logic and design. Cengage Learning:Boston, MA
Yener, M (2015). Professional Java EE Design Patterns. John Wiley & Sons, Inc.
Gordon, S. (2017). Computer Graphics Programming in opengl with Java. Mercury Learning and Information
Butler, A (2017). Java. s.n
Lowe, D (2017). Java all-in-one for dummies. John Wiley and Sons
Saumont, P (2017). Functional Programming in Java: How Functional Techniques Improve Your Java Programs. 1st Edition
Heffelfinger, D. (2017). Java EE 8 Application Development. Packt Puublishing
Murach, J. (2017). Murach’s Java Programming. Mike Murach & Associates,Inc.
Burd, B. (2017). Beginning programming with Java for dummies. John Wiley & Sons, Inc.
Manelli, L. (2017). Developing java web application in a day. s.n.
Klausen, P. (2017). Java 5: Files and Java IO software development (e-book)
Klausen, P. (2017). Java 3 object-oriented programming software development (e-book)
Muller, M (2018). Practical JSF in Java EE 8. Apress