Skip to main content

  | मौसम…

411 समाचार
Public archive edition · Attributed source reporting with original dates · AI drafts are excluded

411 REFERENCE · Wikibooks

Introduction to Programming Languages/Error handling

Reference material by Wikibooks contributors. This retains its source identity and is not 411 news reporting.

The chapter presents several approaches. 1. Preconditions Document: “Do not call this in an invalid state” Example:pop()should only be called if the stack is not empty. Problem If the programmer forgets: the program crashes. 2. Total Definition Make the function always work. Example:if (stack empty) return 0; Problem May hide serious bugs. 3. Fatal Error Simply terminate the program.System.exit(-1); Problem Too rigid. Different clients may want to: ignore; recover; log; terminate. 4. Error Flagging Use error flags. Very common in C. Example:error = true;Then:if (getError()) Problem With Error Flagging Requires constant manual checking. This creates: repetitive code; forgotten checks; silent bugs. 5. Using Exceptions The modern approach. The method detects the error and:throw new Exception(); Advantages of Exceptions 1. Cleaner Code Without thousands of:if(error) 2. Better Separation Main logic separated from error-handling logic. 3. Flexible Handling The caller decides whether to: ignore; recover; terminate. 4. Better Messages Much better than generic failures.