Download presentation
Presentation is loading. Please wait.
1
Fundaments of Game Design
Exception Handling Richard Gesick
2
Objectives Exception handling Try catch finally blocks
Making it robust
3
Exception Handling An exception is an indication of a problem that occurs during a program’s execution. Exception handling enables applications to resolve exceptions. Exception handling enables clear, robust and more fault-tolerant programs. Exception handling helps improve a program’s fault tolerance.
4
Exception Handling Consider the following pseudocode:
Perform a task If the preceding task did not execute correctly Perform error processing Perform next task If the preceding task did not execute correctly Perform error processing … In this pseudocode, we begin by performing a task; then we test whether that task executed correctly. If not, we perform error processing.
5
Exception Handling Exception handling enables programmers to remove error-handling code from the “main line” of the program’s execution. Programmers can decide to handle all exceptions, all exceptions of a certain type or all exceptions of related types. Such flexibility reduces the likelihood that errors will be overlooked.
6
try Block A try block encloses code that might throw exceptions and code that is skipped when an exception occurs. try { open file perform work on file } close file
7
Catch Block When an exception occurs in a try block, a corresponding catch block catches the exception and handles it. At least one catch block must immediately follow a try block. A catch block specifies an exception parameter representing the exception that the catch block can handle. Optionally, you can include a catch block that does not specify an exception type to catch all exception types.
8
Catch Block catch(IO.FileNotFoundException fnfe) { handle file not found (using fnfe object) } catch(Exception e) { handle other type of exception (using e object) close file }
9
Exception Handling- Termination Model
When a method called in a program or the CLR detects a problem, the method or the CLR throws an exception. The point at which an exception occurs is called the throw point If an exception occurs in a try block, program control immediately transfers to the first catch block matching the type of the thrown exception. After the exception is handled, program control resumes after the last catch block.
10
finally Block Programs frequently request and release resources dynamically. Operating systems typically prevent more than one program from manipulating a file. Therefore, the program should close the file (i.e., release the resource) so other programs can use it. If the file is not closed, a resource leak occurs. The finally block is guaranteed to execute regardless of whether an exception occurs.
11
finally Block Local variables in a try block cannot be accessed in the corresponding finally block, so variables that must be accessed in both should be declared before the try block.
12
Reminder Do not place try blocks around every statement that might throw an exception. It’s better to place one try block around a significant portion of code, and follow this try block with catch blocks that handle each possible exception. Then follow the catch blocks with a single finally block. Separate try blocks should be used when it is important to distinguish between multiple statements that can throw the same exception type.
13
Expectations Within your games, you need to have robust exception handling for all of your file processing. If the file doesn’t exist or isn’t found, have a method for creating the file with default data and also for assigning that default data to the necessary components.
14
Expectations If there is a format or data content error, have a process to handle that keeps the game from throwing an exception in the editor.
15
Unity If you are using multiple scenes, when your file processing is in the scene that uses the data and the file isn’t found, Unity may not wait for you to handle the exception/create the file. It may throw an exception and move on without the data if it can. Consider testing for the file’s existence in the opening scene of your game. Causes:???
16
File checker script attached to camera
void Start () { if(!File.Exists(Application.persistentDataPath + "\\" + "scoreFile.txt")) { Debug.Log("creating score file in file checker"); StreamWriter sw = new StreamWriter(Application.persistentDataPath + "\\" + "scoreFile.txt"); sw.WriteLine("A 9"); sw.WriteLine("E 5"); sw.Close(); }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.