DOC PREVIEW
UMD CMSC 132 - Java Review 2 – Errors, Exceptions, Debugging

This preview shows page 1-2-3 out of 10 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 10 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 10 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 10 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 10 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

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

UMD CMSC 132 - Java Review 2 – Errors, Exceptions, Debugging

Documents in this Course
Notes

Notes

8 pages

Recursion

Recursion

12 pages

Sorting

Sorting

31 pages

HTML

HTML

7 pages

Trees

Trees

19 pages

HTML

HTML

18 pages

Trees

Trees

19 pages

Honors

Honors

19 pages

Lecture 1

Lecture 1

11 pages

Quiz #3

Quiz #3

2 pages

Hashing

Hashing

21 pages

Load more
Download Java Review 2 – Errors, Exceptions, Debugging
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Java Review 2 – Errors, Exceptions, Debugging and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Java Review 2 – Errors, Exceptions, Debugging 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?