1Java Review 2 – Errors, Exceptions, DebuggingNelson Padua-PerezChau-Wen TsengDepartment of Computer ScienceUniversity of Maryland, College ParkJava Review TopicsErrorsExceptionsDebugging2Program ErrorsTypes of errorsCompile-time (syntax) errorsRun-time errorsLogic errorsProgram Errors – Compile TimeCompile-time (syntax) errorsErrors in code constructionLexical (typographical), grammatical, typesDetected during compilationUsually easy to correct quicklyExamplesMisspelled keywordMissing or misplaced symbolIncorrect operator for variable type3Program Errors – Run TimeRun-time errorsOperations illegal / impossible to executeDetected during program executionBut not detectable at compile timeTreated as exceptions in JavaExampleDivision by zeroArray index out of boundsUsing null pointerIllegal format conversionProgram Errors – LogicLogic errorsOperations leading to incorrect program stateMay (or may not) lead to run-time errorsProblem in design or implementation of algorithmExamplesComputing incorrect arithmetic valueIgnoring illegal inputHardest error to handleDetect by testing, debugging4Java Review TopicsErrorsExceptionsDebuggingExceptionsRare event outside normal behavior of codeExamplesDivision by zeroAccess past end of arrayOut of memoryNumber input in wrong format (float vs. integer)Unable to write output to fileMissing input file5Exception Handling – Throw ExceptionApproachThrow exceptionExampleA( ) { if (error) throw new ExceptionType(); }B( ) { try { A( ); } catch (ExceptionType e) { ...action... } }Java exception backtracks to caller(s) until matching catch block foundRepresenting Exceptions in JavaExceptions represented asObjects derived from class ThrowableCodepublic class Throwable( ) extends Object {Throwable( ) // No error messageThrowable( String mesg ) // Error messageString getMessage() // Return error mesgvoid printStackTrace( ) { … } // Record methods… // called & location}6Generating & Handling ExceptionsJava primitivesTryThrowCatchFinallyProcedure for using exceptions1. Enclose code generating exceptions in try block2. Use throw to actually generate exception3. Use catch to specify exception handlers4. Use finally to specify actions after exceptionJava Syntaxtry { // try block encloses throwsthrow new eType1(); // throw jumps to catch}catch (eType1 e) { // catch block 1...action... // run if type match} catch (eType2 e) { // catch block 2...action... // run if type match} finally { // final block...action... // always executes}7Exceptions – ExamplesFileNotFoundException ( java.io )Request to open file failsIllegalArgumentException ( java.lang )Method passed illegal / inappropriate argumentIOException ( java.io )Generic I/O errorNullPointerException ( java.lang )Attend to access object using null referenceUnsupportedOperationException ( java.lang )Object does not provide requested operationJava Review TopicsErrorsExceptionsDebugging8DebuggingProcess of finding and fixing software errorsAfter testing detects errorGoalDetermine cause of run-time & logic errorsCorrect errors (without introducing new errors)Similar to detective workCarefully inspect information in programCodeValues of variablesProgram behaviorDebugging – ApproachesClassicInsert debugging statementsTrace program control flowDisplay value of variablesModernIDE (integrated development environment)Interactive debugger9Interactive DebuggerCapabilitiesProvides trace of program executionShows location in code where error encounteredInteractive program executionSingle step through codeRun to breakpointsDisplays values of variablesFor current state of programInteractive DebuggerSingle stepExecute single line of code at a timeWhen executing method, canFinish entire methodExecute first line in methodTedious (or impractical) for long-running programs10Interactive DebuggerBreakpointSpecify location(s) in codeExecute program until breakpoint encounteredCan skip past uninteresting
View Full Document