C# to JavaScript compiler

# 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

  1. Open your browser's Developer Tools (F12).
  2. Enable Source Maps in the settings (usually enabled by default).
  3. Navigate to the "Sources" or "Debugger" tab.
  4. 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).
  5. Set breakpoints in your C# code.
  6. 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.json settings (e.g., strictNullChecks) and ensure external libraries are correctly typed.
  • Missing Source Maps: Ensure h5.json has sourceMap.enabled: true and that the .js.map files are being served correctly by your web server.