Module 5: Error Handling and Debugging Flashcards
What is a syntax error?
A syntax error results from a programmer incorrectly typing in the sequence of characters or tokens to be used in a statement in a programming language.
Syntax errors are typically caught at compile time, but most modern integrated development environments (IDEs) implement features that monitor the code as the programmer types it in, performing syntax error checking and informing the programmer about syntax errors before he or she even attempts to compile the code.
What are logic errors?
A logic error is an error in your algorithm, which causes the algorithm to not work as intended and thereby not solve the problem at hand. Logic errors are the most critical type of errors in your program code. This is because logic errors can be extremely difficult to find.
What is structured exception handling?
It is when you control the alternate flow of execution. Run error handling to: correct the error, notify the user in case they can correct the error, execute clean- up code after the exception.
What is an exception?
It is an event in your application that occurs when something goes wrong. Exceptions cause a change in the flow of execution in your code. If you do not handle the exceptions, the change of flow typically results in the application crashing.
What is a DivideByZeroException?
If your code attempts math division by 0.
What is an InvalidCastException?
When trying to cast from one base type to another, such as casting an object to a string.
What is and IndexOutOfRangeException?
It happens if you attempt to access an element of an array with an invalid index parameter.
What are catch blocks?
It is code that trap any errors that may occur.
What is a finally block?
After an error has been generated and handled, the finally block will still execute before returning the execution back to the application or to the computer. You want this to happen in the event you have a file open and you need to close it before the application ends.
What is throwing an exception?
Throwing an exception is generally what you do when you want the exception to be handled in some location other than the current location in code.
What is a try block?
It is the component of structured exception handling that sets up a piece of code to be monitored for any exceptions that might occur. As MSDN refers to it, the try block contains guarded code. Guarded code is code that might cause an exception.
EX: try { // guarded code }
What is a catch block?
Is responsible for identifying and handling specific, or generic, exceptions. It is where you will specify the exceptions that you want to catch.
EX: static void Main(string[] args) { string errorMessage; try { // Some math functionality here } catch(OverflowException ofEx) { errorMessage = ofEx.Message; } catch(DivideByZeroException dEx) { errorMessage = dEx.Message; } catch(Exception e) { errorMessage = e.Message; }
What is the finally block?
It is the location where you will close the file handle, releasing the resource.
EX: static void Main(string[] args) { try { // Some code to open and access a file here } catch(IOException ioEx) { errorMessage = ioEx.Message; } finally { if(file != null) { file.Close(); } } }
What is the exception handling mechanism?
Catches specific exceptions first; uses exception class as final catch-all; cleans up in Finally.
What is the recommended design strategy for catch blocks?
To place the more specific catch blocks first and the more general catch blocks later.