1 /******************************************************************************
2 
3     Reusable exception base class
4 
5     Copyright:
6         Copyright (c) 2009-2016 dunnhumby Germany GmbH.
7         All rights reserved.
8 
9     License:
10         Boost Software License Version 1.0. See LICENSE_BOOST.txt for details.
11         Alternatively, this file may be distributed under the terms of the Tango
12         3-Clause BSD License (see LICENSE_BSD.txt for details).
13 
14  ******************************************************************************/
15 
16 module ocean.util.ReusableException;
17 
18 import ocean.meta.types.Qualifiers;
19 
20 /*******************************************************************************
21 
22     Enhances Exception with additional mutable message buffer that gets reused
23     each time new message gets thrown.
24 
25     This mutable buffer is only being used if `Exception.msg` is null and calling
26     `ReusableException.enforce` will reset it to null. Using `ReusableException`
27     as an instance to free form `enforce` function will assign to plain `msg`
28     field instead and temporarily shadow mutable one.
29 
30 *******************************************************************************/
31 
32 class ReusableException : Exception
33 {
34     import ocean.core.Exception : ReusableExceptionImplementation;
35 
36     mixin ReusableExceptionImplementation!();
37 
38     /**************************************************************************
39 
40         Constructor
41 
42     ***************************************************************************/
43 
44     this ( ) { super(null); }
45 }
46 
47 version (unittest)
48 {
49     import ocean.core.Test;
50     import ocean.core.Enforce;
51 }
52 
53 unittest
54 {
55     auto ex = new ReusableException;
56 
57     try
58     {
59         enforce(ex, false, "unexpected length for bwa value");
60     }
61     catch (ReusableException ex)
62     {
63         ex.set("Failed to parse number", __FILE__, __LINE__);
64     }
65 }