PriyoScriptPriyoScript
Advanced

Error Handling

Reliable failure handling with prakritiTry, prakritiCatch, prakritiAtEnd, and prakritiThrow.

Theory

Error handling is the mechanism that keeps programs safe when unexpected conditions happen. Instead of crashing silently, PriyoScript lets you explicitly guard risky logic (prakritiTry), capture failures (prakritiCatch), run cleanup (prakritiAtEnd), and create your own failures (prakritiThrow). Good error handling improves reliability, user experience, and debugging speed.

Why this is critical

Programs are not judged by how they run in ideal cases, but by how safely they fail in real cases.

Error Handling API Table

KeywordSyntaxPurpose
prakritiTryprakritiTry { ... }Wrap risky code
prakritiCatchprakritiCatch (err) { ... }Handle thrown/runtime errors
prakritiAtEndprakritiAtEnd { ... }Always run cleanup block
prakritiThrowprakritiThrow "message"Create custom error

Error Object Quick View

Inside catch, err usually exposes:

FieldMeaning
err.messageHuman-readable message
err.codeError code (if available)
err.stageStage hint (syntax/compile/runtime) in advanced diagnostics

Example 1: Basic try/catch/finally

monalisa {
  prakritiTry {
    prakritiThrow "Something failed"
  } prakritiCatch (err) {
    priyoTell.Error("Caught: " + err.message)
  } prakritiAtEnd {
    priyoTell.Info("Cleanup done")
  }
}

Explanation:

  • prakritiThrow creates a failure inside try.
  • catch receives it in err and prints user-friendly message.
  • prakritiAtEnd runs regardless of success/failure.

Output:

Caught: Something failed
Cleanup done

Example 2: Validation before dangerous operation

monalisa {
  lisaaTask divide(a, b) {
    prakritiIf (b == 0) {
      prakritiThrow "Division by zero is not allowed"
    }
    priyoGiveBack a / b
  }

  prakritiTry {
    priyoTell(divide(10, 0))
  } prakritiCatch (err) {
    priyoTell.Warn(err.message)
  }
}

Explanation:

  • Business rule is enforced in function.
  • Caller handles error centrally instead of letting program crash.

Output:

Division by zero is not allowed

Example 3: Continue execution after handled error

monalisa {
  prakritiTry {
    prakritiThrow "Temporary network timeout"
  } prakritiCatch (err) {
    priyoTell.Warn("Recovering: " + err.message)
  }

  priyoTell("Program continues safely")
}

Explanation:

  • Error is handled locally.
  • Execution continues after catch block.

Output:

Recovering: Temporary network timeout
Program continues safely

Example 4: Inspect metadata (message, code)

monalisa {
  prakritiTry {
    prakritiThrow "Custom failure"
  } prakritiCatch (err) {
    priyoTell("Message: " + err.message)
    priyoTell("Code: " + err.code)
  }
}

Explanation:

  • Useful for logging and building user-facing diagnostics.
  • err.code may vary by error source.

Output:

Message: Custom failure
Code: (implementation-specific)

Example 5: Cleanup with prakritiAtEnd

monalisa {
  priyoKeep opened = "yes"

  prakritiTry {
    prakritiThrow "Operation interrupted"
  } prakritiCatch (err) {
    priyoTell.Error(err.message)
  } prakritiAtEnd {
    opened = "no"
    priyoTell.Info("Resource closed: " + opened)
  }
}

Explanation:

  • prakritiAtEnd is the right place for cleanup logic.
  • Cleanup runs even when error occurs.

Output:

Operation interrupted
Resource closed: no

Edge Cases

CaseBehavior
catch without trySyntax/compile error
throw with non-string valueStill handled as throwable value
Error inside catchNew error propagates unless nested try/catch handles it
Error inside prakritiAtEndCan override earlier handled state and propagate
No catch presentError bubbles to runtime/CLI printer

Debug tip

Keep try blocks small. Catch close to the risky operation so error context remains clear.

Comparison With Other Languages

  • JavaScript/Python/Java: same conceptual try-catch-finally model.
  • C: no native exceptions, usually return-code based error handling.
  • PriyoScript: similar model with humanized diagnostics and code-based explain flow.

Practice Questions

Concept Check

  1. When should you use prakritiAtEnd instead of placing cleanup after catch? 2. Why is throwing a clear message better than generic text like "failed"? 3. What happens when an error is thrown but no catch handles it?

Last updated on

On this page