Docsity

Review of Java Programming Errors, Exceptions & Debugging by Padua-Perez & Tseng, Univ. of, Study notes of Computer Science

Toggle sidebar
bg1
1
Java Review 2 โ€“ Errors,
Exceptions, Debugging
Nelson Padua-Perez
Chau-Wen Tseng
Department of Computer Science
University of Maryland, College Park
Java Review Topics
Errors
Exceptions
Debugging
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Review of Java Programming Errors, Exceptions & Debugging by Padua-Perez & Tseng, Univ. of and more Study notes Computer Science in PDF only on Docsity!

Java Review 2 โ€“ Errors,

Exceptions, Debugging

Nelson Padua-Perez

Chau-Wen Tseng

Department of Computer Science

University of Maryland, College Park

Java Review Topics

Errors

Exceptions

Debugging

Program Errors

Types of errors

Compile-time (syntax) errors Run-time errors Logic errors

Program Errors โ€“ Compile Time

Compile-time (syntax) errors

Errors in code construction Lexical (typographical), grammatical, types Detected during compilation Usually easy to correct quickly

Examples

Misspelled keyword Missing or misplaced symbol Incorrect operator for variable type

Java Review Topics

Errors

Exceptions

Debugging

Exceptions

Rare event outside normal behavior of code

Examples

Division by zero Access past end of array Out of memory Number input in wrong format (float vs. integer) Unable to write output to file Missing input file

Exception Handling โ€“ Throw Exception

Approach

Throw exception

Example

A( ) {

if (error) throw new ExceptionType(); } B( ) { try { A( ); } catch (ExceptionType e) { ...action... } }

Java exception backtracks to caller(s) until matching catch block found

Representing Exceptions in Java

Exceptions represented as

Objects derived from class Throwable

Code

public class Throwable( ) extends Object { Throwable( ) // No error message Throwable( String mesg ) // Error message String getMessage() // Return error mesg void printStackTrace( ) { โ€ฆ } // Record methods โ€ฆ // called & location }

Exceptions โ€“ Examples

FileNotFoundException ( java.io )

Request to open file fails

IllegalArgumentException ( java.lang )

Method passed illegal / inappropriate argument

IOException ( java.io )

Generic I/O error

NullPointerException ( java.lang )

Attend to access object using null reference

UnsupportedOperationException ( java.lang )

Object does not provide requested operation

Java Review Topics

Errors

Exceptions

Debugging

Debugging

Process of finding and fixing software errors

After testing detects error

Goal

Determine cause of run-time & logic errors Correct errors (without introducing new errors)

Similar to detective work

Carefully inspect information in program Code Values of variables Program behavior

Debugging โ€“ Approaches

Classic

Insert debugging statements Trace program control flow Display value of variables

Modern

IDE (integrated development environment) Interactive debugger

Interactive Debugger

Breakpoint

Specify location(s) in code Execute program until breakpoint encountered Can skip past uninteresting code