Introduction to Error Handling
Let’s take a look at some code used to generate a background and foreground. If you’ve followed the lesson on Drawing Backgrounds & Foregrounds this code should look a bit familiar to you.
Now, let’s look at the resource.res file and check our images that we’re calling.
As you can see, we’ve got assets assigned to bg2 and fg2 which we’re calling in the body of our code. Yet, we know this is a tutorial on error handling so something must be amiss somewhere.
Let’s attempt to do the divine trifecta of Save/Clean/Compile and see what happens.
res/resources.res: error on line 4
C:/SGDK/bin/mkdir.exe -p out/res/
C:/SGDK/bin/gcc.exe -x assembler-with-cpp -Wa,--register-prefix-optional,--bitwise-or -DSGDK_GCC -m68000 -Wall -Wextra -Wno-shift-negative-value -Wno-main -Wno-unused-parameter -fno-builtin -fms-extensions -Iinc -Isrc -Ires -IC:/SGDK/inc -IC:/SGDK/res -BC:/SGDK/bin -O3 -fuse-linker-plugin -fno-web -fno-gcse -fno-unit-at-a-time -fomit-frame-pointer -flto -c res/resources.rs -o out/res/resources.o
gcc.exe: error: res/resources.rs: No such file or directory
gcc.exe: warning: '-x assembler-with-cpp' after last input file has no effect
gcc.exe: fatal error: no input files
compilation terminated.
make: *** [out/res/resources.o] Error 1
Looking at the image below, you’ll see the text that appears in Terminal. It’s important to ensure that you’re using CDM (Command Prompt) Terminal rather than PowerShell as it could cause some unexpected issues.
Use the (+) icon to add “cmd” to the list and I recommend removing PowerShell from the list (assuming your version of Visual Studio Code defaults to using PowerShell).
Looking at the Error Handler text, you’ll notice that I’ve highlighted the first line of text in yellow. It’s directly referencing where the issue is. It references Line 4 of the resources.res file. The highlighted green text offers an explanation of what is wrong.
Line 4 is our fg2 image file. The format appears to be correct. We’ve defined the file type as IMAGE, given it a reference name with fg2, linked to the image file, and defined the image processing options with BEST and ALL just as we did with other images. Yet, something is off with Line 4.
Looking at the folder structure on the left side of VSCode, specifically at the res folder, we’re going to locate all our images in the res\TILES folder. First, we want to check the spelling of the file names and make sure they’re placed between quotation marks. The spelling on clouds.png looks fine. Still, something is off. If we compare our entry for Line 3 to Line 4 we should see a difference.
Line 3’s filepath is “tiles/back1sg1.png”
Line 4’s filepath is “clouds.png”
Line 3 references the TILES folder and then points to back1sg1.png whereas Line 4 does not do this. Either we move the cloudSGpng image to the root of the res folder or we modify the filepath to include Tiles/.
Note: filepath file names are NOT case sensitive
Let’s try the latter and see if we can compile properly this time. Like washing your hands after you pee, get in the habit of doing the Save/Clean/Compile waltz after changing anything in the resources.res code.
Doing so, we can see that the code is now compiling
properly.
Next, let’s see what happens if there is an issue with a line of code in main.c
What I did was to change bg2 to bg3 and removed the semicolon at the end of the setPalette function for PAL1. If you were to just Clean and Compile it may compile properly. That’s because the changes haven’t been saved yet. If you S/C/C you should get error messages to return (see below).
/res -BC:/SGDK/bin -O3 -fuse-linker-plugin -fno-web -fno-gcse -fno-unit-at-a-time -fomit-frame-pointer -flto -MMD -c src/main.c -o out/src/main.o
src/main.c: In function 'main':
src/main.c:17:26: error: 'bg3' undeclared (first use in this function)
PAL_setPalette(PAL0, bg3.palette->data, DMA);
^~~
src/main.c:17:26: note: each undeclared identifier is reported only once for each function it appears in
src/main.c:24:5: error: expected ';'
before 'while'
while(1)
^~~~~
make: *** [out/src/main.o] Error 1
rm res/resources.rs
Looking at the section of text highlighted in yellow, this is pointing to line 24 in filepath src/main.c and it’s stating that “bg3” is undeclared. Most often, this is hinting at a typo in your reference, or it could be that we did not remember to add a resource named bg3 in our resources.res file. In this instance, it’s a deliberate instance of the former rather than the latter. We done goof’d on purpose.
The final thing I want to focus on is the highlighted text in green. In the image above, you’ll notice that when hovering your mouse over the text on the left it appears as a link. You can use the (ctrl + left-click) combination to jump right to the line referenced by the link. This will save you a ton of time from having to scroll through a lot of code and then hunting around for where the issue is.
Also, you may have expected the error handler to cite the semicolon that is missing on the Line 18 function and specifically point you to Line 18. Yet, it didn’t do that. It’s pointing to a semicolon needing to be added to the front of the while(1) function to make it a proper function.
The reason for this is that the semicolon denotes a stopping point of sorts to a function. What the error handler thinks is happening is that your code from Line 18 is carrying on past the commented-out code to the ‘w’ in while(1) because it’s expecting a semicolon to end the function. So long as you carefully read the error handling text, it should indicate that you need to scan the body of VSCode start from the ‘w’ in while(1) and work your way up each line to see if you can figure out where the issue is.
As a general rule, you can save yourself a little bit of time and just look at the end of your functions to see if they have a semicolon or not and eventually one line should stand out.
WARNING: Do NOT put in proprietary code into an AI Assistant unless you have a registered account.
With that disclaimer out of the way, AI is useful for parsing through code. To give you an example of what ChatGPT can do I’m going to paste some faulty code into ChatGPT and see what it returns.
Sample code
ChatGPT Output
✅ Fixes Needed
ind += bg2.tileset->numTile; // instead of bg1
ind += fg2.tileset->numTile; // instead of fg1
PAL_setPalette(PAL1, fg2.palette->data, DMA); // Add semicolon here
VDP_DRAWTEXT(...) // Should be: VDP_drawText("WHY DO AI...", 5, 5);
VDP_drawText("But why tho?", 10, 10);. // <-- Extra period
✅ Cleaned-Up Version
#include <genesis.h>
#include <resources.h>
u16 ind = TILE_USER_INDEX;
int main()
{
VDP_drawImageEx(BG_B, &bg2, TILE_ATTR_FULL(PAL0, FALSE, FALSE, FALSE, ind), 0, 0, FALSE, TRUE);
ind += bg2.tileset->numTile;
VDP_drawImageEx(BG_A, &fg2, TILE_ATTR_FULL(PAL1, FALSE, FALSE, FALSE, ind), 0, 0, FALSE, TRUE);
ind += fg2.tileset->numTile;
PAL_setPalette(PAL0, bg2.palette->data, DMA);
PAL_setPalette(PAL1, fg2.palette->data, DMA);
VDP_setScreenWidth256();
VDP_setScreenWidth320();
VDP_drawText("WHY DO AI LOGOS LOOK LIKE BOOTYHOLES?", 5, 5);
VDP_drawText("But why tho?", 10, 10);
while(1)
{
SYS_doVBlankProcess(); // Use VDP_waitVSync(); for SGDK < 1.60
}
return 0;
}
✅ Suggestions
ChatGPT was able to determine the issues in the code and the cleaned-up version of the code does indeed compile. The suggestions are often helpful though the last suggestion is clearly not to be trusted. Furthermore, be leery of falling into the vibecoding trap. Use AI as a tool not as a crutch. If you’re not backing up your code prior to attempting to code a new idea you have, there’s the real possibility that you could go down a rabbit hole with AI that could potentially do more harm than good.
As you code you will make mistakes. It’s never a bad idea to document these mistakes as having them as a reference point later could be useful. Though my tutorials are quite bloated with commented code that’s strictly for teaching purposes, it doesn’t hurt to add references to the bottom of your code base. You never know when you’ll find yourself starting your code at four libations in and in the zone and then you’re at libation number 16 and put some questionable code in there. You’ll probably want a reference note or twenty in there to make sure your great ideas aren’t lost to sobriety.
Good luck, keep coding, and game on.