Creado por Ruth Hyndman
hace casi 9 años
|
||
Pregunta | Respuesta |
Why do we need to error trap in program code? | No matter the programmer or the program, errors and mistakes can occur. If errors occur and they are not handled in the correct way then the program will crash which can have a massive detrimental effect. For example, on October 25th 2015, Barclays computer glitch affects customers as computer problem stopped some customers making payments or withdrawing cash with repairs delayed before the clocks went back one hour. |
What are the three main types of errors you experience when programming? | - Compilation errors - Runtime errors - Logical errors |
What is a Compilation error? | Compilation errors, also known as compiler errors, are errors that prevent your program from running. When you press F5 to run a program, Visual Studio compiles your code into a binary language that the computer understands. If the Visual Studio compiler comes across code that it does not understand, it issues a compiler 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. Syntax errors: errors due to the fact that the syntax of the language is not respected. Semantic errors: errors due to an improper use of program statements. |
Give examples of Compilation errors. | Example 1 Displaying various syntax errors. - misspelling keywords Example 2 Small semantic errors int x -= 1; int y += 1; |
What 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. |
What 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. |
What 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 |
What 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 |
Give 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 |
How 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. |
Handler for Multiple Exceptions | To 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. |
Multiple/Multi Exception Handler | A 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 |
Finally Block | When 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. |
¿Quieres crear tus propias Fichas gratiscon GoConqr? Más información.