CS 162 Lecture 19: CS 162 – Lecture 19 – Exception Handling
CS 162 – Lecture 19 – Exception Handling
Exception Handling
o We are talking about c++, other languages are different!
o Deals with unusual circumstances that may require different reactions
o THIS NOT YOUR TYPICAL ERROR HANDLING
▪ Handling bad input that you can predict will be common should not use
Exception Handing
▪ Save Exception Handling for edge cases
▪ What if bad allocation of memory?
Try-throw-catch
o Code in the catch block will execute and then it will continue with the rest of
your program will execute
▪ Oe it hits the ath lok, ad it’s thro, the rest of the try lok ill
not execute
o Try {
▪ Some code, typically the function or piece which may encounter the
unusual error, the basic algorithm for when things go well
▪ Throw Exception_parameter;
▪ Maybe more code
o }
o catch (type_of_exception_param e) {
▪ code to handle the exception
o }
Details
o Not athig o ariale aes, it’s athig o the type
▪ Looks for type match
o You can only have one type that you are catching
▪ Only ONE parameter
▪ Have to create a different catch block if want more
find more resources at oneclass.com
find more resources at oneclass.com