Difference between revisions of "Handling Exceptions"
(→DetermininG the type of exception) |
|||
(One intermediate revision by the same user not shown) | |||
Line 1: | Line 1: | ||
+ | '''Definition:''' An exception is an event, which occurs during the execution of a program, that disrupts the normal flow of the program's instructions. | ||
+ | |||
Even if you program compiles successfully, errors can still be generated at runtime during the execution of the program. When this occurs, visual studio will present you with an '''Exception'''. Sometimes, the generation of these errors cannot feasibly be avoided and thus requires the use of '''Exception Handling'''. | Even if you program compiles successfully, errors can still be generated at runtime during the execution of the program. When this occurs, visual studio will present you with an '''Exception'''. Sometimes, the generation of these errors cannot feasibly be avoided and thus requires the use of '''Exception Handling'''. | ||
Line 32: | Line 34: | ||
=== Example One: === | === Example One: === | ||
+ | You can handle any exception by just using "<tt>catch( Exception )</tt>". | ||
+ | <tabber> | ||
+ | C#=<syntaxhighlight lang="csharp" line> | ||
+ | class Program { | ||
+ | static void Main(string[] args) { | ||
+ | int a; | ||
+ | int b = 0; | ||
+ | try { | ||
+ | a = 1 / b; | ||
+ | Console.WriteLine("Exception not caught! :C"); | ||
+ | } | ||
+ | catch ( Exception ) { | ||
+ | Console.WriteLine("Exception caught!"); | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | </syntaxhighlight> | ||
+ | |-| | ||
+ | Output=<tt style='color:brown'>Exception caught!</tt> | ||
+ | </tabber> | ||
+ | |||
+ | === Example Two: === | ||
+ | |||
+ | |||
Below is an example of how exception handling can be used to detect the existence of [[Text Files]], and if one exists, load the username from the file. | Below is an example of how exception handling can be used to detect the existence of [[Text Files]], and if one exists, load the username from the file. | ||
Line 58: | Line 84: | ||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− |
Latest revision as of 11:27, 21 March 2019
Definition: An exception is an event, which occurs during the execution of a program, that disrupts the normal flow of the program's instructions.
Even if you program compiles successfully, errors can still be generated at runtime during the execution of the program. When this occurs, visual studio will present you with an Exception. Sometimes, the generation of these errors cannot feasibly be avoided and thus requires the use of Exception Handling.
Exception handling is done by using the following code to control what code to run in case the program encounters a specific exception
1 try
2 {
3 //code to attempt to execute (may result in error)
4 }
5 catch(Exception)
6 {
7 //what to do if the code fails
8 //this defaults to 'throw;' which will cause your program to exit unexpectedly.
9 }
The 'Exception' part of the code can be left as is, but will catch ALL exceptions encountered within that piece of code, this can be a bad thing if a genuine error occurs and it is generally a good idea to determine the type of exception that you are encountering, and placing a reference to that there.
Contents
Determining the type of exception
When an exception is unhandled, visual studio will present a dialogue box presenting you with the type of exception and some possible solutions. In this image, the ExceptioN is System.IndexOutOfRangeException:
To catch this exception you would use the code:
1 catch(IndexOutOfRangeException)
2 {
3 //code to execute
4 }
Examples of Exception Handling
Example One:
You can handle any exception by just using "catch( Exception )".
1 class Program {
2 static void Main(string[] args) {
3 int a;
4 int b = 0;
5 try {
6 a = 1 / b;
7 Console.WriteLine("Exception not caught! :C");
8 }
9 catch ( Exception ) {
10 Console.WriteLine("Exception caught!");
11 }
12 }
13 }
Exception caught!
Example Two:
Below is an example of how exception handling can be used to detect the existence of Text Files, and if one exists, load the username from the file.
1 class Program
2 {
3 static void Main(string[] args)
4 {
5 try
6 {
7 using (StreamReader Load = new StreamReader("Program/Preferences.txt"))
8 {
9 //Try to read from preferences.txt
10 Username = Load.ReadLine();
11 }
12 }
13 catch(NullReferenceException) //Null reference exception occurs when the file cannot be found
14 {
15 using (StreamWriter Save = new StreamWriter("Program/Preferences.txt"))
16 {
17 //Preferences.txt does not exist, so create one
18 Save.Write(Username);
19 }
20 }
21 }
22 }