Guest User

Untitled

a guest
Sep 19th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.72 KB | None | 0 0
  1. import std.conv, std.stdio, std.exception;
  2.  
  3. void main(string[] args) {
  4. enforce(args.length > 1, "Usage: foo.exe filename");
  5.  
  6. double[] nums;
  7.  
  8. // Process a text file with one number per line into an array of doubles,
  9. // ignoring any malformed lines.
  10. foreach(line; File(args[1]).byLine()) {
  11. try {
  12. nums ~= to!double(line);
  13. } catch(ConvError) {
  14. // Ignore malformed lines.
  15. }
  16. }
  17.  
  18. // Do stuff with nums.
  19. }
  20.  
  21. boolean isNonZeroNumber = false;
  22.  
  23. try {
  24. if(StringUtils.isNotBlank(s) && new BigDecimal(s).compareTo(BigDecimal.ZERO) != 0) {
  25. b = true;
  26. }
  27. } catch(NumberFormatException e) {
  28. //swallow this exception; b stays false.
  29. }
  30.  
  31. return b;
  32.  
  33. try {
  34. bytes[] hw = "Hello World".getBytes("UTF-8");
  35. }
  36. catch(UnsupportedCodingException e) {
  37. }
  38.  
  39. import util.control.Exception.ignoring
  40.  
  41. val numStrings = Array("a", "2", "5")
  42. for(a <- numStrings) {
  43. ignoring(classOf[NumberFormatException]) {
  44. println(a.toInt)
  45. }
  46. }
  47.  
  48. 2
  49. 5
  50.  
  51. try
  52. {
  53. DoSomething(); //This should throw exception if everything works well
  54. Assert.Fail('Expected exception was not thrown');
  55. }
  56. catch(<whatever exception>)
  57. {
  58. //Expected to get here
  59. }
  60.  
  61. /* Check if the process is still alive; exitValue() throws an exception if it is */
  62. try {
  63. p.exitValue();
  64. processPool.remove(p);
  65. }
  66. catch (IllegalThreadStateException e) { /* expected behaviour */ }
  67.  
  68. try
  69. {
  70. doWork();
  71. }
  72. catch(BenignException x)
  73. {
  74. _log.Debug("Error doing work: " + x.Message);
  75. }
  76.  
  77. try
  78. {
  79. // execution code here
  80. }
  81. catch(Exception e)
  82. {
  83. // do nothing here
  84. }
  85. finally
  86. {
  87. // close db connection
  88. // close file io resource
  89. // close memory stream
  90. // etc...
  91. }
  92.  
  93. this.currentDate = new Date();
  94. try {
  95. this.currentDate = $.datepicker.parseDate(this.options.hiddenDateFormat, this.element.val());
  96. } catch(_) {}
  97.  
  98. // If an exception happens, it doesn't matter. Log the initial value or the new value
  99.  
  100. function logId(person) {
  101. let id = 'No ID';
  102. try {
  103. id = person.data.id;
  104. } catch {}
  105. console.log(id);
  106. }
  107.  
  108. function getter(obj){}
  109.  
  110. function objChecker()
  111. {
  112. try
  113. {
  114. /* Check argument length and constructor type */
  115. if (getter.length === 1 && getter.constructor === Function)
  116. {
  117. console.log("Correct implementation");
  118. return true;
  119. }
  120. else
  121. {
  122. console.log("Wrong implementation");
  123. return false;
  124. }
  125. }
  126. catch(e)
  127. {
  128. return NaN;
  129. }
  130. finally
  131. {
  132. console.log(JSON.stringify(getter))
  133. }
  134. }
  135.  
  136. // Test the override of the getter method
  137. getter = getter;
  138. objChecker();
  139. getter = [];
  140. objChecker();
  141. getter = {};
  142. objChecker();
  143. getter = RegExp;
  144. objChecker();
  145. getter = Function;
  146. objChecker();
Add Comment
Please, Sign In to add comment