Bifalgorithm post · Computer science one · Creating Custom Exceptions

How can a try-catch statement handle an exception from readNumber()?

How can a try-catch statement handle an exception from readNumber()? (Computer science one · Creating Custom Exceptions) — "Elsewhere in the code, we can surro…

Ideas in this flowchart

  • Gestion d'une exception de readNumber() avec try-catch
  • Un appel à readNumber() est entouré d'une instruction try-catch
  • Le bloc try appelle readNumber()
  • Le bloc catch traite l'exception Exception $e
  • Un message d'erreur est affiché sur la sortie standard
  • Le programme se termine avec le statut 1
  • $e->getMessage() affiche le message associé à la création de l'exception
  • Dans cet exemple, le message est « Invalid input! »
  • La décision de conception considère cette erreur comme fatale
  • Le bloc catch aurait pu traiter l'erreur autrement
Feed/

How can a try-catch statement handle an exception from readNumber()?

No cover

Channel

Computer science one

1 page

"Elsewhere in the code, we can surround a call to readNumber() in a try-catch statement.\n\ntry {\n\nreadNumber();\n\n} catch(Exception $e) {\n\nprintf(\"Error: exception encountered: \" . $e->getMessage());\n\nexit(1);\n\n}\n\nIn this example, we’ve simply displayed an error message to the standard output and exited the program. We’ve made the design decision that this error should be fatal. We could have chosen to handle this error differently in the catch block. The $e->getMessage() prints the message that the exception was created with. In this case, \"Invalid input!\"."

Flowchart preview
👍
0
0 comments0 shares

Sign in to view and post comments.