"""
ExceptionAnalyzer
Generated by Eden via recursive self-improvement
2025-10-28 06:35:35.625159
"""

class ExceptionAnalyzer:
    """A class to analyze exceptions and provide actionable insights."""
    
    def __init__(self):
        self.error_context = {}
        
    def capture_exception(self, e):
        """
        Captures exception details and stores them for analysis.
        Args:
            e (Exception): The exception object to be analyzed.
        Returns:
            str: A summary of the captured exception.
        """
        self.error_context = {
            'type': type(e).__name__,
            'message': str(e),
            'traceback': e.__traceback__
        }
        return f"Captured {self.error_context['type']}: {self.error_context['message']}"
    
    def analyze(self):
        """
        Analyzes the captured exception and suggests potential fixes.
        Returns:
            str: Analysis and suggested fixes.
        """
        analysis = ""
        
        if self.error_context.get('type') == 'ZeroDivisionError':
            if 'division by zero' in self.error_context['message']:
                analysis += "Zero Division Error:\n"
                analysis += "Solution: Avoid division by zero. Check divisor before operation.\n"
            elif 'divisor is zero' in self.error_context['message']:
                analysis += "Zero Division Error:\n"
                analysis += "Solution: Ensure the divisor is not zero.\n"
        
        elif self.error_context.get('type') == 'FileNotFoundError':
            if 'file not found' in self.error_context['message']:
                analysis += "File Not Found Error:\n"
                analysis += f"Solution: Verify file path '{self.error_context['message'].split("'")[1]}" 
                analysis += " exists.\n"
        
        elif self.error_context.get('type') == 'IndexError':
            if 'list index out of range' in self.error_context['message']:
                analysis += "Index Error:\n"
                analysis += "Solution: Check list boundaries before accessing elements.\n"
        
        return analysis
    
    def __str__(self):
        """Returns a string representation of the error context and analysis."""
        return f"Error Context: {self.error_context}\nAnalysis: {self.analyze()}"


if __name__ == "__main__":
    # Example usage
    analyzer = ExceptionAnalyzer()
    
    try:
        # Simulate some errors
        1 / 0
    except ZeroDivisionError as e:
        print(analyzer.capture_exception(e))
        print(analyzer.analyze())
        
    try:
        open('nonexistentfile.txt', 'r')
    except FileNotFoundError as e:
        analyzer = ExceptionAnalyzer()
        print(analyzer.capture_exception(e))
        print(analyzer.analyze())
    
    try:
        my_list = []
        print(my_list[5])
    except IndexError as e:
        analyzer = ExceptionAnalyzer()
        print(analyzer.capture_exception(e))
        print(analyzer.analyze())