#
Exceptions & Debugging
h5 provides standard C# exception handling and debugging capabilities in the browser.
#
Exception Handling
You can use standard try, catch, and finally blocks to handle exceptions in your C# code.
try
{
ThrowException();
}
catch (InvalidOperationException ex)
{
Console.WriteLine("Caught specific exception: " + ex.Message);
}
catch (Exception ex)
{
Console.WriteLine("Caught general exception: " + ex.Message);
}
finally
{
Console.WriteLine("Finally block executed");
}
public void ThrowException()
{
throw new InvalidOperationException("Something went wrong!");
}
Exceptions thrown in h5 are mapped to standard JavaScript Error objects, preserving the message, stack trace (where possible), and type information.
#
Debugging
Since h5 compiles C# to JavaScript, debugging usually involves inspecting the generated JavaScript code. However, h5 supports Source Maps, which allow you to debug your original C# source files directly in the browser's developer tools.
#
Enabling Source Maps
Ensure source map generation is enabled in your h5.json configuration:
{
"sourceMap": {
"enabled": true
}
}
When enabled, h5 generates .js.map files alongside the JavaScript output. These files map the generated JavaScript back to your C# source code lines.
#
Using Source Maps
- Open your browser's Developer Tools (F12).
- Enable Source Maps in the settings (usually enabled by default).
- Navigate to the "Sources" or "Debugger" tab.
- You should see your C# files listed in the file tree (often under
webpack://or a similar virtual path if using a bundler, or directly under the domain). - Set breakpoints in your C# code.
- Run your application. The debugger will pause at your C# breakpoints.
You can inspect variables, step through code, and view the call stack just as you would in Visual Studio, but directly within the browser.
#
Common Issues
- "undefined is not a function": This often happens when calling methods on null references or incorrect JavaScript interop. Check your
h5.jsonsettings (e.g.,strictNullChecks) and ensure external libraries are correctly typed. - Missing Source Maps: Ensure
h5.jsonhassourceMap.enabled: trueand that the.js.mapfiles are being served correctly by your web server.