//  ============================================================
//  System.Exception is the base class of all Cminor exceptions.
//  System.Exception.ToString() method returns a string that
//  contains the class name of the exception, a stack trace, and
//  the error message while System.Exception.Message contains
//  just the error message.
//
//  System.SystemException is the base class of Cminor system
//  exceptions just as System.NullReferenceException,
//  System.IndexOutOfRangeException and
//  System.InvalidCastException, that are throw by the virtual
//  machine when member of null tried to be accessed, an array
//  index is out of range, or type of object or value does not
//  allow it to be casted to the type it is tried to cast to.
//  ============================================================

using System.Text;

namespace System
{
    public class Exception
    {
        public Exception(string message)
        {
            this.message = message;
        }
        public void SetStackTrace(string stackTrace)
        {
            this.stackTrace = stackTrace;
        }
        public string Message
        {
            get { return message; }
        }
        public string StackTrace
        {
            get { return stackTrace; }
        }
        public override string ToString()
        {
            StringBuilder s = new StringBuilder();
            s.Append(base.ToString()).Append(": ");
            if (!string.IsNullOrEmpty(message))
            {
                s.Append(message);
            }
            if (!string.IsNullOrEmpty(stackTrace))
            {
                s.Append(":\n").Append(stackTrace);
            }
            return s.ToString();
        }
        private string message;
        private string stackTrace;
    }

    public class SystemException : Exception
    {
        public SystemException(string message) : base(message)
        {
        }
    }

    public class NullReferenceException : SystemException
    {
        public NullReferenceException(string message) : base(message)
        {
        }
    }

    public class IndexOutOfRangeException : SystemException
    {
        public IndexOutOfRangeException(string message) : base(message)
        {
        }
    }

    public class ArgumentException : SystemException
    {
        public ArgumentException(string message) : base(message)
        {
        }
    }

    public class ArgumentOutOfRangeException : ArgumentException
    {
        public ArgumentOutOfRangeException(string message) : base(message)
        {
        }
    }

    public class ArgumentNullException : ArgumentException
    {
        public ArgumentNullException(string message) : base(message)
        {
        }
    }

    public class InvalidCastException : SystemException
    {
        public InvalidCastException(string message) : base(message)
        {
        }
    }

    public class FileSystemException : SystemException
    {
        public FileSystemException(string message) : base(message)
        {
        }
    }

    public class SocketException : SystemException
    {
        public SocketException(string message) : base(message)
        {
        }
        public int ErrorCode
        {
            get { return errorCode; }
            set { errorCode = value; }
        }
        private int errorCode;
    }

    public class LogicException : Exception
    {
        public LogicException(string message) : base(message)
        {
        }
    }

    public class StackOverflowException : SystemException
    {
        public StackOverflowException(string message) : base(message)
        {
        }
    }

    [vmf=rethrow]
    public extern void Rethrow(Exception exception);
}