eli5: In programming, how does using “try and except/catch” blocks stop the program from crashing?

607 views

eli5: In programming, how does using “try and except/catch” blocks stop the program from crashing?

In: 10

20 Answers

Anonymous 0 Comments

try/catch/finally statements have become a modern programming standard.

For example you could be trying to connect to a database, if the connection fails you could throw an error. Basically throwing an error means “An error has occured that my code function either can’t deal with, or doesn’t want to deal with, if you’d like to deal with it, catch it”

With errors you can either catch all errors a function or code block may throw, or just specific erros that you’re expecting. If an error your try block doesn’t catch, it may get caught by a try/catch block further up the code chain – if it never gets caught you get a program error.

You are viewing 1 out of 20 answers, click here to view all answers.