1<?xml version="1.0" encoding="utf-8"?>
2<html xml:lang="en-US" lang="en-US">
3<head>
4    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
5    <meta http-equiv="Content-Style-Type" content="text/css" />
6    <link rel="stylesheet" href="../../../css/page.css" type="text/css" />
7<title>File System: Error Handling</title>
8</head>
9<body>
10<h1>File System: Error Handling</h1>
11<h2>Table of Contents</h2>
12    <ul>
13<li><a href="#Summary">Overview</a></li>
14      <ul>
15<li><a href="#Summary_Result">Result Handling</a></li>
16<li><a href="#Summary_Error">Errors That Must Not Be Allowed to Occur in Retail Products</a></li>
17<li><a href="#Summary_Api">Functions for File and Directory Operations</a></li>
18      </ul>
19<li><a href="#Mounting">Handling Mount Errors</a></li>
20      <ul>
21<li><a href="#Mounting_RomArchive">ROM Archives</a></li>
22<li><a href="#Mounting_SaveDataArchive">Save Data Archives</a></li>
23<li><a href="#Mounting_ExtSaveDataArchive">Expanded Save Data Archives</a></li>
24      </ul>
25<li><a href="#FileAndDirectory">Handling Errors During File and Directory Operations</a></li>
26      <ul>
27<li><a href="#FileAndDirectory_RomArchive">ROM Archives</a></li>
28<li><a href="#FileAndDirectory_SaveDataArchive">Save Data Archives</a></li>
29<li><a href="#FileAndDirectory_ExtSaveDataArchive">Expanded Save Data Archives</a></li>
30      </ul>
31<li><a href="#History">Revision History</a></li>
32    </ul>
33<a name="Summary"><h2>Overview</h2></a>
34    <div class="section">
35This page describes error handling for the file system.
36<a name="Summary_Result"><h3>Result Handling</h3></a>
37      <div class="section">
38FS functions return many different types of <CODE>Result</CODE> values. These are handled by the following mechanism rather than one value at a time. The following example shows how to handle all errors belonging to the <CODE><a href="../../../nn/fs/ResultNotFound/Overview.html">ResultNotFound</a></CODE> category. <code><pre>
39if(result &lt;= nn::fs::ResultNotFound())
40{
41    // This can catch nn::fs::ResultMediaNotFound as well.
42}
43</pre>The <CODE>Result</CODE> objects returned by <CODE>fs</CODE> functions are forbidden from using the arithmetic operators <CODE>==</CODE> or <CODE>!=</CODE>. Using these causes a build error. Also note that the following two examples are equivalent. <code><pre>
44if(nn::fs::ResultNotFound::Includes(result))
45{
46    ...
47}
48</pre></code> <code><pre>
49if(nn::fs::ResultNotFound() &gt;= result)
50{
51    ...
52}
53</pre></code>
54<p><font color="red">Do not directly handle any <CODE>Result</CODE> values that are not mentioned in the Function Reference Manual.</font></p>
55      </div>
56<a name="Summary_Error"><h3>Errors That Must Not Be Allowed to Occur in Retail Products</h3></a>
57      <div class="section">
58The following <CODE>Result</CODE> values indicate errors that are caused by application bugs and other such problems. You must fix your program so that these do not occur in retail products.
59        <ul>
60<li><a href="../../../nn/fs/ResultOutOfResource/Overview.html"><CODE>ResultOutOfResource</CODE></a></li>
61<li><a href="../../../nn/fs/ResultAccessDenied/Overview.html"><CODE>ResultAccessDenied</CODE></a></li>
62<li><a href="../../../nn/fs/ResultInvalidArgument/Overview.html"><CODE>ResultInvalidArgument</CODE></a></li>
63<li><a href="../../../nn/fs/ResultNotInitialized/Overview.html"><CODE>ResultNotInitialized</CODE></a></li>
64<li><a href="../../../nn/fs/ResultAlreadyInitialized/Overview.html"><CODE>ResultAlreadyInitialized</CODE></a></li>
65<li><a href="../../../nn/fs/ResultUnsupportedOperation/Overview.html"><CODE>ResultUnsupportedOperation</CODE></a></li>
66        </ul>
67The <CODE>Result</CODE> values listed above may be caused by user operations or it may be impossible to get rid of them entirely during development. You must handle errors that can be caused by the user. Some functions may also specify errors that must not occur in retail products, even though the corresponding <CODE>Result</CODE> values are not among those listed above. For more information, see the documentation for each of these functions in the Function Reference Manual.
68      </div>
69<a name="Summary_Api"><h3>Functions for File and Directory Operations</h3></a>
70      <div class="section">
71There are two types of functions used to perform file and directory operations: <CODE>Try</CODE> functions (which have the prefix <CODE>Try</CODE> added to the function name), and non-<CODE>Try</CODE> functions (which do not contain this prefix). <CODE>Try</CODE> functions return a <CODE>Result</CODE> as the return value, unless an fatal internal error occurs. Instead of returning <CODE>Results</CODE>, non-<CODE>Try</CODE> functions are implemented to display an error screen whenever an internal error returns, regardless of whether the error is fatal. Control does not return from the function in this case.<br /><br />Internal errors never occur for ROM archives because of their characteristics. For ROM archives, implementation omissions in error handling can be avoided by using the non-<CODE>Try</CODE> functions. (During development, make sure to eliminate situations in which an error could occur because of an attempt to open a nonexistent file.)<br /> <br />For archives other than ROM archives, you must always check for errors that can result from the user's operations. One typical example is removal of the Game Card by the user, which can lead to corrupted files. As a result, <CODE>Try</CODE> functions should always be used for error handling. Do not use non-<CODE>Try</CODE> functions because they can easily jump to error screens as a result of user operations.
72      </div>
73    </div>
74<a name="Mounting"><h2>Handling Mount Errors</h2></a>
75    <div class="section">
76The following simplified C++ code provides guidelines for handling mount errors.
77<a name="Mounting_RomArchive"><h3>ROM Archives</h3></a>
78      <div class="section">
79<code><pre>
80// An error will cause an internal transition to the error display screen.
81// The application does not need to perform error handling.
82// NOTE: When developing an application, confirm that you are passing enough working memory as an argument.
83//       Also check your Omakefile and RSF file to determine whether a ROMFS is created at build time.
84nn::fs::MountRom();
85</pre></code>
86      </div>
87<a name="Mounting_SaveDataArchive"><h3>Save Data Archives</h3></a>
88      <div class="section">
89<code><pre>
90nn::Result result = nn::fs::MountSaveData();
91if(result.IsFailure())
92{
93    if(result &lt;= nn::fs::ResultNotFormatted())
94    {
95        // Formatting required.
96        // CAUSE: Unformatted save data region.
97        // NOTE: You must always format a save data region before mounting it for the first time.
98        result = nn::fs::FormatSaveData();
99    }
100    else if(result &lt;= nn::fs::ResultBadFormat())
101    {
102        // Formatting required.
103        // CAUSE: Invalid file format.
104        result = nn::fs::FormatSaveData();
105    }
106    else if(result &lt;= nn::fs::ResultVerificationFailed())
107    {
108        // Formatting required.
109        // CAUSE: Corrupted or tampered-with save data.
110        // NOTE: This state is never the result of user operations when there is automatic redundancy.
111        //       This error may be caused by user operations (such as removing a card or turning off the system while data is being written) when there is no automatic redundancy.
112        result = nn::fs::FormatSaveData();
113    }
114    else if(result &lt;= nn::fs::ResultOperationDenied())
115    {
116        // As of CTR-SDK 2.1, this error is handled by the system. Treat it as an unexpected error.
117    }
118    else
119    {
120        // Unexpected error if other than the above
121        NN_ERR_THROW_FATAL_ALL(result);
122    }
123    // Troubleshooting after formatting
124    if(result.IsFailure())
125    {
126        // Here, the act of formatting save data never fails.
127        // NOTE: When developing your application, confirm that the maxFiles and maxDirectories arguments are not too big.
128        //       Also confirm that the content of the RSF file is correct.
129        NN_ERR_THROW_FATAL_ALL(result);
130    }
131    else
132    {
133        // Remount
134        result = nn::fs::MountSaveData();
135        if(result.IsFailure())
136        {
137            // The same as when making the first call.
138            // (Supplement) A mount operation never fails immediately after data has been successfully formatted.
139        }
140    }
141}
142</pre></code>
143      </div>
144<a name="Mounting_ExtSaveDataArchive"><h3>Expanded Save Data Archives</h3></a>
145      <div class="section">
146<code><pre>
147nn::Result result = nn::fs::MountExtSaveData();
148if(result.IsFailure())
149{
150    if(result &lt;= nn::fs::ResultNotFound())
151    {
152        if(result &lt;= nn::fs::ResultMediaNotFound())
153        {
154            // An SD Card has not been inserted.
155            // Note: This error is also returned if the SD Card is damaged or if some medium other than an SD Card is inserted.
156            //        In such cases, an insertion event registered by <CODE>RegisterSdmcInsertedEvent</CODE> is signaled.
157        }
158        else
159        {
160            // The specified expanded save data does not exist and must be created.
161            // NOTE: Confirm that the ID you specified is correct when developing your application.
162            result = nn::fs::CreateExtSaveData();
163        }
164    }
165    else if(result &lt;= nn::fs::ResultNotFormatted())
166    {
167        // You must recreate the expanded save data.
168        // CAUSE: Failed to create expanded save data.
169        result = nn::fs::DeleteExtSaveData();
170        if(result.IsSuccess())
171        {
172            result = nn::fs::CreateExtSaveData();
173        }
174    }
175    else if(result &lt;= nn::fs::ResultBadFormat())
176    {
177        // You must format the SD Card.
178    }
179    else if(result &lt;= nn::fs::ResultVerificationFailed())
180    {
181        // You must recreate the expanded save data.
182        // CAUSE: The save data has been corrupted or tampered with.
183        result = nn::fs::DeleteExtSaveData();
184        if(result.IsSuccess())
185        {
186            result = nn::fs::CreateExtSaveData();
187        }
188    }
189    else if(result &lt;= nn::fs::ResultOperationDenied())
190    {
191        if(result &lt;= nn::fs::ResultWriteProtected())
192        {
193            // The SD Card is write-protected.
194            // NOTE: This occurs if the data recovery sequence is run during a mount operation.
195        }
196        else if(result &lt;= nn::fs::ResultMediaAccessError())
197        {
198            // This error is only returned when the cause is hardware-related, such as a loose connection.
199            // In such cases, recovery may be possible by re-inserting the SD Card, restarting the system, or some other action.
200        }
201        else
202        {
203            // The file or directory on the SD card may be read-only.
204            // NOTE: This occurs if the data recovery sequence is run during a mount operation.
205        }
206    }
207    else
208    {
209        // Unexpected error if other than the above
210        // However, make sure that accessing extended save data does not cause a FATAL error.
211    }
212    // Troubleshooting after data has been created
213    if(result.IsFailure())
214    {
215        if(result &lt;= nn::fs::ResultNotEnoughSpace())
216        {
217            // The SD Card does not have the necessary free space.
218        }
219        else if(result &lt;= nn::fs::ResultNotFormatted())
220        {
221            // An error has occurred, interrupting the process of data creation.
222        }
223        else if(result &lt;= nn::fs::ResultOperationDenied())
224        {
225            if(result &lt;= nn::fs::ResultWriteProtected())
226            {
227                // The SD Card is write-protected.
228            }
229            else if(result &lt;= nn::fs::ResultMediaAccessError())
230            {
231                // This error is only returned when the cause is hardware related, such as a poor connection.
232                //       In such cases, recovery may be possible by retrying, re-inserting the Game Card, restarting the system, or some other action.
233            }
234            else
235            {
236                // The directory on the SD card may be read-only.
237                // NOTE: This occurs if the data recovery sequence is run during a mount operation.
238            }
239        }
240        else
241        {
242            // Unexpected error if other than the above
243            // However, make sure that accessing extended save data does not cause a FATAL error.
244            // NOTE: When developing your application, confirm that valid values are passed to iconData and iconDataSize.
245        }
246    }
247    else
248    {
249        // Remount
250        result = nn::fs::MountExtSaveData();
251        if(result.IsFailure())
252        {
253            // The same as when making the first call.
254            // (Supplement) As long as the SD Card is not removed, mount operations carried out immediately after the data is created will never fail.
255        }
256    }
257}
258</pre></code>
259      </div>
260    </div>
261<a name="FileAndDirectory"><h2>Handling Errors During File and Directory Operations</h2></a>
262    <div class="section">
263The following simplified C++ code provides a guideline for handling errors during file and directory operations.<br /><br /> There are two types of functions used to perform file and directory operations: <CODE>Try</CODE> functions (which have the prefix <CODE>Try</CODE> added to the function name), and non-<CODE>Try</CODE> functions (which do not contain this prefix). For details, see <a href="#Summary_Api">Functions for File and Directory Operations</a>.
264<a name="FileAndDirectory_RomArchive"><h3>ROM Archives</h3></a>
265      <div class="section">
266<code><pre>
267nn::fs::XXX();
268// Because errors are handled internally by non-<CODE>Try</CODE> functions, you do not need to receive <CODE>Result</CODE> values.
269</pre></code>
270      </div>
271<a name="FileAndDirectory_SaveDataArchive"><h3>Save Data Archives</h3></a>
272      <div class="section">
273<code><pre>
274result = nn::fs::TryXXX();
275if(result.IsFailure())
276{
277    if(result &lt;= nn::fs::ResultNotFound())
278    {
279        // The specified path does not exist.
280        // NOTE: This error is returned when creating a file or directory only if the problem involves an incorrect archive name.
281        //       You do not need to handle this if, by design, it does not occur.
282    }
283    else if(result &lt;= nn::fs::ResultAlreadyExists())
284    {
285        // The specified path already exists.
286        // NOTE: This error is not returned when files and directories are deleted.
287        //       You do not need to handle this if, by design, it does not occur.
288    }
289    else if(result &lt;= nn::fs::ResultVerificationFailed())
290    {
291        // You must delete and recreate this file or directory.
292        // CAUSE: The save data has been corrupted or tampered with.
293        // NOTE: This state will never be caused by user operations (such as removing a card or turning off the system while data is being written) when there is automatic redundancy.
294        //       This error occurs even by user operations when there is no automatic redundancy.
295        //       If this is not resolved, you must format the save data.
296        // NOTE: This error is returned when reading a region that has never been written to.
297        // Be careful after executing the functions <CODE>nn::fs::TryCreateFile</CODE> and <CODE>nn::fs::FileStream::TrySetSize</CODE>.
298    }
299    else if(result &lt;= nn::fs::ResultOperationDenied())
300    {
301        if(result &lt;= nn::fs::ResultMediaAccessError())
302        {
303            // As of CTR-SDK 2.1, this error is handled by the system. Treat it as an unexpected error.
304            NN_ERR_THROW_FATAL_ALL(result);
305        }
306        else
307        {
308            // Access was denied for some reason.
309            // EXAMPLE: You attempted to delete a file that was open elsewhere.
310            //           You attempted to move a file or directory so that it would span archives.
311            //           You attempted to delete the root directory.
312            // NOTE: You do not need handling for the above situations if, by design, they will not occur.
313        }
314    }
315    else if(result &lt;= nn::fs::ResultNotEnoughSpace())
316    {
317        // There is no available space in the archive or on the storage media.
318        // NOTE: This error may occur when creating a file or directory, or changing the file size.
319        //       If this error occurs when you create a file, delete the file because it might be corrupted.
320        //       If this error occurs when you change the size of a file, the file may have grown to the maximum possible size.
321    }
322    else
323    {
324        // Unexpected error if other than the above
325        NN_ERR_THROW_FATAL_ALL(result);
326    }
327}
328</pre></code>
329      </div>
330<a name="FileAndDirectory_ExtSaveDataArchive"><h3>Expanded Save Data Archives</h3></a>
331      <div class="section">
332<code><pre>
333result = nn::fs::TryXXX();
334if(result.IsFailure())
335{
336    if(result &lt;= nn::fs::ResultNotFound())
337    {
338        if(result &lt;= nn::fs::ResultMediaNotFound())
339        {
340            // An SD Card has not been inserted.
341            // Note: This error is also returned if the SD Card is damaged or if some medium other than an SD Card is inserted.
342            //        In such cases, an insertion event registered by <CODE>RegisterSdmcInsertedEvent</CODE> is signaled.
343        }
344        else
345        {
346            // The specified path does not exist.
347            // NOTE: This error is returned when creating a file or directory only if the problem involves an incorrect archive name.
348            // NOTE: Expanded save data can also be accessed from other applications,
349            // so handling is not required for this error.
350        }
351    }
352    else if(result &lt;= nn::fs::ResultAlreadyExists())
353    {
354        // The specified path already exists.
355        // NOTE: This error is not returned when files and directories are deleted.
356        // NOTE: Because expanded save data can also be accessed from other applications, this error does not need to be handled.
357        //
358    }
359    else if(result &lt;= nn::fs::ResultVerificationFailed())
360    {
361        // You must delete and recreate this file or directory.
362        // CAUSE: The save data has been corrupted or tampered with.
363        // NOTE: If this is not resolved, you need to recreate the expanded save data.
364        // NOTE: This error is returned when reading a region that has never been written to.
365        // Be careful after executing the functions <CODE>nn::fs::TryCreateFile</CODE> and <CODE>nn::fs::FileStream::TrySetSize</CODE>.
366    }
367    else if(result &lt;= nn::fs::ResultArchiveInvalidated())
368    {
369        // You must remount the archive.
370        // CAUSE: The SD Card was removed, making the archive invalid.
371        // NOTE: Close all open files, call the nn::fs::Unmount function, and then remount the archive.
372    }
373    else if(result &lt;= nn::fs::ResultOperationDenied())
374    {
375        if(result &lt;= nn::fs::ResultWriteProtected())
376        {
377            // The SD Card is write-locked.
378            // NOTE: This error occurs when you write to an SD Card. This does not occur when you simply open a file.
379        }
380        else if(result &lt;= nn::fs::ResultMediaAccessError())
381        {
382            // This error is only returned when the cause is hardware related, such as a poor connection.
383            // In such cases, recovery may be possible by re-inserting the SD Card, restarting the system, or some other action.
384        }
385        else
386        {
387            // Access was denied for some reason other than those above.
388            // EXAMPLES: You attempted to write to a file that is read-only.
389            //           You attempted to delete a file that was open elsewhere.
390            //           You attempted to move a file or directory so that it would span archives.
391            //           You attempted to delete the root directory.
392            // (Note) Because the content of expanded save data can be modified even by other applications (depending on operations),
393            // or a user may have spoofed data on the SD card using a PC, this error must always be handled
394        }
395    }
396    else if(result &lt;= nn::fs::ResultNotEnoughSpace())
397    {
398        // There is no available space in the archive or on the storage media.
399        // NOTE: This error may occur when creating a file or directory, or changing the file size.
400        //       If this error occurs when you create a file, delete the file because it might be corrupted.
401        //       If this error occurs when you change the size of a file, the file may have grown to the maximum possible size.
402    }
403    else
404    {
405        // Unexpected error if other than the above
406        // However, make sure that accessing extended save data does not cause a FATAL error.
407    }
408}
409</pre></code>
410      </div>
411    </div>
412<a name="History"><h2>Revision History</h2></a>
413    <div class="section">
414      <dl class="history">
415        <dt>2011/03/14</dt>
416<dd>Removed <CODE>ResultMediaAccessError</CODE> from the results when mounting save data and when operating on files and directories.</dd>
417        <dt>2011/03/07</dt>
418<dd>Deleted <CODE>ResultArchiveInvalidated</CODE> from the errors that can occur in functions to create or delete expanded save data.</dd>
419        <dt>2011/02/28</dt>
420<dd>Revised error handling for mounting ROM archives in accordance with changes to API.</dd>
421        <dt>2011/02/07</dt>
422<dd>Changed how mounting expanded save data is handled from recreating the archive using <CODE>ResultBadFormat</CODE> to formatting the SD card.</dd>
423<dd>Deleted <CODE>ResultBadFormat</CODE> from how manipulating expanded save data files and directories are handled.</dd>
424<dd>Revised code so that unexpected errors now result in <CODE>NN_ERR_THROW_FATAL_ALL</CODE>.</dd>
425        <dt>2011/01/05</dt>
426<dd>Deleted <CODE>ResultArchiveInvalidated</CODE> from the errors that can occur when mounting the expanded save data.</dd>
427<dd>Changed the <CODE>Result</CODE> from <CODE>ResultMediaAccessError</CODE> to <CODE>ResultMediaNotFound</CODE> when a damaged SD Card is inserted.</dd>
428        <dt>2010/12/14</dt>
429<dd>Added errors that must not be allowed to occur in retail products.</dd>
430<dd>Added <CODE>ResultNotFormatted</CODE> and <CODE>ResultArchiveInvalidated</CODE> to error handling when expanded save data is created.</dd>
431        <dt>2010/12/13</dt>
432<dd>Split up error handling for file and directory operations by archive type.</dd>
433<dd>Changed how <CODE>ResultOperationDenied</CODE> is handled when expanded save data is mounted.</dd>
434        <dt>2010/12/09</dt>
435<dd>Added <B>Functions for File and Directory Operations</B>.</dd>
436        <dt>2010/12/02</dt>
437<dd>Removed <CODE>ResultNotFound</CODE> from save data error handling.</dd>
438        <dt>2010/11/31</dt>
439<dd>Added <CODE>ResultNotEnoughSpace</CODE> to the errors occurring during file operations.</dd>
440        <dt>2010/11/25</dt>
441<dd>Added <CODE>ResultNotFormatted</CODE> to error handling for expanded save data.</dd>
442        <dt>2010/11/13</dt>
443<dd>Initial version.</dd>
444      </dl>
445    </div>
446<hr><p>CONFIDENTIAL</p></body>
447</html>
448