Docsity

Types of Programming Errors - Web Design and Development - Lecture Slides, Slides of Web Design and Development

Partial preview of the text

Download Types of Programming Errors - Web Design and Development - Lecture Slides and more Slides Web Design and Development in PDF only on Docsity!

Web Design & Development

Lecture 7

Intro to Exceptions

Exceptions

 An exception is an event that describes an unusual or erroneous

situation at runtime.

 Exceptions are wrapped up as objects

 A program can deal with an exception in one of three ways:

 i gnore it

 handle it where it occurs

 handle it an another place in the program

 An error is also represented as an object in Java, but usually represents

an unrecoverable situation and should not be caught

Why Use Exceptions?

 Uses of exception handling

 Process exceptions from program components

 Handle exceptions in a uniform manner in large

projects

 Remove error-handling code from “main line” of

execution

 What if Exception is not handled?

 Might terminate program execution

Exceptions Types

 Two Types

 Checked

 Must be caught or declared in a throws clause

 Compile will issue an error if not handled appropriately

 Subclasses of Exception other than subclasses of RuntimeException.

 Other arrive from external factors, and cannot be solved by debugging

 Communication from an external resource – e.g. a file server or database

Throwable

Exception Error

IOException

ArrayIndexOutOfBoundsException InputMismatchException

NullPointerException

Runtime Exception

AWTError ThreadDeath

ClassCastException

OutOf MemoryError

ArithmeticException

Checked Exceptions

UnChecked Exceptions Error

How are Java exceptions handled

 Basic Java exception handling is managed via keywords: try ,

catch, finally, throw, throws.

 try block

 Code that could generate errors put in try blocks

 catch block

 Code for error handling enclosed in a catch clause

 finally block

 The finally clause always executes

 Resources that are opened may need to be closed during

exception handling

 Appears after the last catch block

 It wil not execute if System.exit(0) occurs first

How are Java exceptions handled

 throw

 To manually throw an exception, use the keyword throw.

 throws

 throws exception out of the method, requiring it to be caught and handled by an appropriate exception handler

 Any exception that is thrown out of a method must be specified as such by a throws clause.

Examples

Example: Unchecked Exceptions

public class UcException {

public static void main(String args[ ]) {

System.out.println(args[0]);

}

}

Example: Unchecked Exceptions

public class UcException {

public static void main(String args[ ]) {

System.out.println(args[0]);

}

}

Example: Unchecked Exceptions

Modification

public class UcException {

public static void main(String args[ ]) {

try {

System.out.println(args[0]);

}catch (IndexOutOfBoundsException ex) {

System.out.println(“You forget to pass command line argument”);

Example: Checked Exceptions

import java.io.*;

public class CException {

public static void main(String args[ ]) {

FileReader fr = new FileReader (“input.txt”);

BufferedReader br = new BufferedReader(fr);

//read the line

String s = br.readLine();

System.out.println(s);

Example: Checked Exceptions

compile & execute