ExceptionHandler API Documentation
Overview
The ExceptionHandler
class is a comprehensive exception management system within the Krypton Toolkit Suite. It provides centralized exception handling capabilities with multiple display options, logging functionality, and integration with the Krypton UI framework.
Namespace: Krypton.Toolkit
Access Level: internal
(for internal toolkit use)
Public API: KryptonExceptionHandler
(public wrapper)
Architecture
The exception handling system consists of several interconnected components:
ExceptionHandler (internal)
↓
KryptonExceptionHandler (public API)
↓
KryptonExceptionDialog (UI wrapper)
↓
VisualExceptionDialogForm (actual form)
Core Classes
1. ExceptionHandler (Internal)
The core exception handling class that provides the fundamental exception management functionality.
Location: Source/Krypton Components/Krypton.Toolkit/Tooling/ExceptionHandler.cs
Constructor
public ExceptionHandler()
Creates a new instance of the ExceptionHandler class.
Methods
CaptureException
public static void CaptureException(
Exception exception,
string title = "Exception Caught",
[CallerFilePath] string callerFilePath = "",
[CallerLineNumber] int lineNumber = 0,
[CallerMemberName] string callerMethod = "",
bool showStackTrace = false,
bool? useExceptionDialog = true,
bool? showExceptionDialogCopyButton = false,
bool? showExceptionDialogSearchBox = false)
Purpose: Captures and displays exceptions with comprehensive debugging information.
Parameters:
exception
(Exception): The exception to capture and displaytitle
(string): Title for the exception dialog (default: "Exception Caught")callerFilePath
(string): Automatically populated with the calling file path using[CallerFilePath]
lineNumber
(int): Automatically populated with the calling line number using[CallerLineNumber]
callerMethod
(string): Automatically populated with the calling method name using[CallerMemberName]
showStackTrace
(bool): Whether to include stack trace in the message (default: false)useExceptionDialog
(bool?): Whether to use the advanced KryptonExceptionDialog (default: true)showExceptionDialogCopyButton
(bool?): Whether to show copy button in the dialog (default: false)showExceptionDialogSearchBox
(bool?): Whether to show search functionality in the dialog (default: false)
Behavior:
- If
useExceptionDialog
is true (default), displays the exception usingKryptonExceptionDialog.Show()
- If
useExceptionDialog
is false, displays a simple message box with formatted exception details - Automatically captures caller information using C# compiler attributes
- Provides fallback error handling for any exceptions during the capture process
Example Usage:
try
{
// Some operation that might throw an exception
ProcessData();
}
catch (Exception ex)
{
ExceptionHandler.CaptureException(ex, "Data Processing Error", showStackTrace: true);
}
PrintStackTrace
public static void PrintStackTrace(Exception exception, string fileName)
Purpose: Writes exception details and stack trace to a file.
Parameters:
exception
(Exception): The exception to logfileName
(string): Path to the file where the exception details will be written
Behavior:
- Creates the file if it doesn't exist
- Writes both
exception.ToString()
andexception.StackTrace
to the file - Properly disposes of file resources
- If file operations fail, calls
CaptureException
with stack trace enabled
Example Usage:
try
{
// Some operation
}
catch (Exception ex)
{
ExceptionHandler.PrintStackTrace(ex, @"C:\Logs\error.log");
}
PrintExceptionStackTrace
public static void PrintExceptionStackTrace(Exception exception, string fileName)
Purpose: Writes only the stack trace portion of an exception to a file.
Parameters:
exception
(Exception): The exception whose stack trace to logfileName
(string): Path to the file where the stack trace will be written
Behavior:
- Similar to
PrintStackTrace
but only writesexception.StackTrace
- Useful when you only need the call stack without the full exception details
- Includes proper error handling and resource disposal
2. KryptonExceptionHandler (Public API)
A public wrapper that provides access to the internal ExceptionHandler functionality.
Location: Source/Krypton Components/Krypton.Toolkit/Controls Toolkit/KryptonExceptionHandler.cs
Methods
CaptureException
public static void CaptureException(Exception exception,
string title = "Exception Caught",
[CallerFilePath] string callerFilePath = "",
[CallerLineNumber] int lineNumber = 0,
[CallerMemberName] string callerMethod = "",
bool showStackTrace = false,
bool? useExceptionDialog = true)
Purpose: Public interface to the internal exception capture functionality.
Note: This method has fewer parameters than the internal version, focusing on the most commonly used options.
PrintStackTrace
public static void PrintStackTrace(Exception exception, string fileName)
Purpose: Public interface to the internal stack trace printing functionality.
PrintExceptionStackTrace
public static void PrintExceptionStackTrace(Exception exception, string fileName)
Purpose: Public interface to the internal exception stack trace printing functionality.
3. KryptonExceptionDialog (UI Component)
A static class that provides access to the advanced exception dialog UI.
Location: Source/Krypton Components/Krypton.Toolkit/Controls Toolkit/KryptonExceptionDialog.cs
Methods
Show
public static void Show(Exception exception, bool? showCopyButton, bool? showSearchBox)
Purpose: Displays an exception using the advanced VisualExceptionDialogForm.
Parameters:
exception
(Exception): The exception to displayshowCopyButton
(bool?): Whether to show the copy button (default: true)showSearchBox
(bool?): Whether to show the search functionality (default: true)
4. VisualExceptionDialogForm (UI Implementation)
The actual Windows Forms dialog that displays exception information in a user-friendly format.
Location: Source/Krypton Components/Krypton.Toolkit/Controls Visuals/VisualExceptionDialogForm.cs
Features
- Tree View Display: Shows exception hierarchy with inner exceptions
- Search Functionality: Allows searching through exception details
- Copy to Clipboard: One-click copying of exception details
- Formatted Display: Clean, readable presentation of exception information
- Responsive Design: Adapts to different screen sizes
- Localized Strings: Uses KryptonManager.Strings for internationalization
Key Properties
_showCopyButton
(bool?): Controls copy button visibility_showSearchBox
(bool?): Controls search functionality visibility_exception
(Exception?): The exception being displayed_originalNodes
(List): Backup of original tree nodes for search functionality
Configuration and Dependencies
GlobalStaticValues Integration
The ExceptionHandler integrates with GlobalStaticValues
for default configuration:
internal const bool DEFAULT_USE_STACK_TRACE = true;
internal const bool DEFAULT_USE_EXCEPTION_MESSAGE = true;
internal const bool DEFAULT_USE_INNER_EXCEPTION = true;
String Localization
The exception dialog supports localization through KryptonManager.Strings.ExceptionDialogStrings
:
WindowTitle
: Dialog window titleExceptionDetailsHeader
: Header for exception details sectionExceptionOutlineHeader
: Header for exception outline sectionType
: Label for exception typeMessage
: Label for exception messageStackTrace
: Label for stack trace sectionInnerException
: Label for inner exception sectionNone
: Text displayed when no inner exception existsSearchBoxCueText
: Placeholder text for search box
Usage Patterns
Basic Exception Handling
try
{
// Risky operation
PerformOperation();
}
catch (Exception ex)
{
KryptonExceptionHandler.CaptureException(ex);
}
Advanced Exception Handling with Custom Title
try
{
// Operation
}
catch (InvalidOperationException ex)
{
KryptonExceptionHandler.CaptureException(ex, "Invalid Operation Detected", showStackTrace: true);
}
File-Based Exception Logging
try
{
// Operation
}
catch (Exception ex)
{
// Log to file
KryptonExceptionHandler.PrintStackTrace(ex, @"C:\Logs\ApplicationErrors.log");
// Also show to user
KryptonExceptionHandler.CaptureException(ex, "Application Error");
}
Custom Exception Dialog Configuration
try
{
// Operation
}
catch (Exception ex)
{
// Use internal handler for full control
ExceptionHandler.CaptureException(
ex,
"Custom Error Title",
showStackTrace: true,
useExceptionDialog: true,
showExceptionDialogCopyButton: true,
showExceptionDialogSearchBox: true
);
}
Error Handling and Resilience
The ExceptionHandler includes comprehensive error handling:
- File Operations: All file writing operations are wrapped in try-catch blocks
- Resource Management: Proper disposal of file streams and writers
- Fallback Mechanisms: If the primary exception handling fails, it falls back to basic error reporting
- Recursive Protection: Prevents infinite loops when exception handling itself throws exceptions
Integration Points
With KryptonMessageBox
When useExceptionDialog
is false, the ExceptionHandler uses KryptonMessageBox.Show()
to display exception information in a simple message box format.
With KryptonManager
The exception dialog integrates with the KryptonManager for:
- String localization
- Theme management
- Global configuration
With GlobalStaticValues
Uses global constants for default behavior configuration.
Best Practices
When to Use Each Method
- Use
CaptureException
for user-facing error handling where you want to display the exception - Use
PrintStackTrace
for comprehensive logging that includes full exception details - Use
PrintExceptionStackTrace
for lightweight logging that only needs the call stack - Use
KryptonExceptionDialog.Show
directly when you need fine-grained control over the dialog appearance
Performance Considerations
- The exception dialog is modal and blocks the UI thread
- File operations are synchronous and may cause brief delays
- Consider using async patterns for file logging in high-performance scenarios
Security Considerations
- Exception details may contain sensitive information
- File logging should use secure paths and proper permissions
- Consider sanitizing exception messages before logging in production environments
Troubleshooting
Common Issues
- File Access Denied: Ensure the application has write permissions to the log file directory
- Dialog Not Appearing: Check that the UI thread is not blocked
- Missing Caller Information: Ensure you're calling from C# code (not reflection or dynamic code)
Debugging Tips
- Use
showStackTrace: true
to get detailed call stack information - Check the
GlobalStaticValues.DEFAULT_USE_STACK_TRACE
setting - Verify that
KryptonManager
is properly initialized for localized strings
Future Enhancements
Potential areas for future development:
- Async Support: Add async versions of file logging methods
- Custom Formatters: Allow custom exception formatting
- Remote Logging: Support for sending exceptions to remote logging services
- Performance Metrics: Add timing and performance tracking for exception handling
- Exception Filtering: Add filtering capabilities to exclude certain exception types