Flashcards
Exception handling
Cards in this deck (15)
- 1Why do we need to error trap in program code?If errors occur and they are not handled in the correct way then the program will crash which can have a massive detrimental effect.
- 2What are the three main types of errors you experience when programming?- Compilation errors
- Runtime errors
- Logical errors - 3What is a compilation error?Compilation errors, also known compiler errors, are errors that prevent your program from running.
- 4How does a compilation error occur?When you run a program, the SDE/IDE compiles your code into a binary language that the computer understands. If the SDE/IDE compiler comes across code that it does not understand, it issues a compiler error.
- 5What causes a compilation error?Most compiler errors are caused by mistakes that you make when typing code. For example, you might misspell a keyword, or leave out some necessary punctuation.
- 6Examples of Compilation errors.Example 1
Displaying various syntax errors.
- misspelling keywords
Example 2
Small semantic error
int x -= 1; - 7What is a Runtime compiler?Run-time errors are errors that occur while your program runs. These typically occur when your program attempts an operation that is impossible to carry out. An example of this is division by zero. Suppose you had the following statement:
Speed = Miles / Hours
If the variable Hours has a value of 0, the division operation fails and causes a run-time error. The program must run in order for this error to be detected, and ifHours contains a valid value, it will not occur at all.
When a run-time error does occur, you can use the debugging tools in Visual Studio to determine the cause. An extremely useful tool provided by the IDE is the breakpoint which can be used to pause the running of your program at a desired location. This would allow you to inspect values of variables and generally get a closer look at what your code is doing. - 8What is a logical error?Logic errors are errors that prevent your program from doing what you intended it to do. Your code may compile and run without error, but the result of an operation may produce a result that you did not expect. For example, you might have a variable named FirstName that is initially set to a blank string. Later in your program, you might concatenate FirstName with another variable named LastName to display a full name. If you forgot to assign a value to FirstName, only the last name would be displayed, not the full name as you intended.
Logic errors are generally the hardest to find and fix, but again Visual Studio has debugging tools that make this job easier. - 9What is an Exception?An Exception is an object delivered by the Exception class. This Exception class is exposed by the System.Exception namespace.
Definition: An exception is an event, which occurs during the execution of a program, that disrupts the normal flow of the program's instructions.
Exceptions are used to avoid system failure in an unexpected manner.
Exception handles the failure situation that may arise.
All the exceptions in the .NET framework are derived from the System.Exception class - 10What is an Unhanded Exception?If an exception is thrown and there is no handler to deal with the exception, this situation is called an Un-Handled exception situation.
If the code is executed, the program will break and you will get an unhandled exception dialog.
Example of Unhandled Exceptions
Example code that causes an exception that is unhandled is shown below.
This is due to that fact that the code is trying to convert a string to a number that contains a letter - 11Give examples of different Exceptions and explain why they are needed.There are all sorts of ways you can write code that C# doesn’t know how to deal with. It would be difficult to troubleshoot your problems if your program simply gave a generic error message.
Therefore there are different types of Exceptions that occur.
Examples exceptions include:
Exception (Generic)
NullReferenceException
DivideByZeroException
FormatException
InvalidCastException - 12How do we handle exceptions?In programs, we can implement ways to catch errors so they do not crash the program and one of the ways you can do this is by using a “Try Catch” block.
To handle the exception, we need to place the code within a try block.
When an exception occurs inside the try block, the control looks for the catch block and raises an exception that is handled in the catch block.
Below is the simple skeleton for the try and catch block:
NB: A try simply starts with the keyword try it is then followed by a set of curly brackets and everything within those curly brackets is part of that try block. A try must always be followed by at least one catch or one finally.
The skeleton example above handles any exception but the main disadvantage is that we don’t know what exception is raised and who raised the exception.
You can specify the type of exception in the catch block and provide a variable name for the exception.
This allows the access to access the Exception object. - 13Handler for Multiple ExceptionsTo handle multiple exceptions, you can place multiple catch blocks for a single try block.
When an exception is thrown, the type of the exception is examined against each catch block and when a match occurs, the exception enters into the catch block and gets handled. - 14Multiple/Multi Exception HandlerA catch is created by typing in the catch keyword and opening a pair of parenthesises. Within these parenthesises we need to declare a variable to store our type of exception. This is followed by a set of curly brackets.
In the second catch we are using a generic exception type called Exception. This will be able to catch any exception thrown. Because it catches any exception throw it must be placed below more specific exceptions - 15Finally BlockWhen there is code that needs to run e.g. close a stream even if an exception occurs, a finally block is used to facilitate this.
The finally block always gets executed even if an exception occurs or not.
Information
- Description
- Flashcards for Exception Handling unit of the AS part of the SSD course by CCEA.
- Created by
- a St George
- Views
- 9
- Tags
- History
-
- Created by Ruth Hyndman · Jan 5, 2016
- Copied by a St George · May 7, 2022
- Rating