/core/exception.d

http://github.com/wilkie/djehuty · D · 115 lines · 56 code · 14 blank · 45 comment · 0 complexity · 03a912e7b834dab8ed78bac6df35a5d1 MD5 · raw file

  1. /*
  2. * exception.d
  3. *
  4. * This module defines common exceptions.
  5. *
  6. * Author: Dave Wilkinson
  7. * Originated: August 20th, 2009
  8. *
  9. */
  10. module core.exception;
  11. import core.string;
  12. import core.definitions;
  13. /*
  14. class Exception {
  15. this(string msg, string file = "", ulong line = 0) {
  16. _msg = msg.dup;
  17. _file = file.dup;
  18. _line = line;
  19. }
  20. string name() {
  21. return this.classinfo.name.dup;
  22. }
  23. string message() {
  24. return _msg.dup;
  25. }
  26. string file() {
  27. return _file;
  28. }
  29. ulong line() {
  30. return _line;
  31. }
  32. string toString() {
  33. return this.name() ~ " caught at " ~ _file ~ "@" ~ toStr(_line) ~ ": " ~ _msg;
  34. }
  35. private:
  36. char[] _msg;
  37. char[] _file;
  38. ulong _line;
  39. }
  40. */
  41. // Exceptions for IO
  42. abstract class IOException : Exception {
  43. this(string msg) {
  44. super(msg);
  45. }
  46. static:
  47. class CreationFailure : IOException {
  48. this(string filename) {
  49. super(filename ~ " could not be created.");
  50. }
  51. }
  52. class ExistenceFailure : IOException {
  53. this(string filename) {
  54. super(filename ~ " not found.");
  55. }
  56. }
  57. class PermissionFailure : IOException {
  58. this(string filename) {
  59. super(filename ~ " has the wrong permissions for the operation.");
  60. }
  61. }
  62. }
  63. // Exceptions for data structures
  64. abstract class DataException : Exception {
  65. this(string msg) {
  66. super(msg);
  67. }
  68. static:
  69. class OutOfElements : DataException {
  70. this(string objectName) {
  71. super("Out of items in " ~ objectName);
  72. }
  73. }
  74. class OutOfBounds : DataException {
  75. this(string objectName) {
  76. super("Index out of bounds in " ~ objectName);
  77. }
  78. }
  79. class ElementNotFound : DataException {
  80. this(string objectName) {
  81. super("Element does not exist in " ~ objectName);
  82. }
  83. }
  84. }
  85. abstract class MemoryException : Exception {
  86. this(string msg) {
  87. super(msg);
  88. }
  89. static:
  90. class OutOfMemory : MemoryException {
  91. this() {
  92. super("Out of memory");
  93. }
  94. }
  95. }