Path: csiph.com!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: Bo Persson Newsgroups: comp.lang.c++ Subject: Re: newbie question: exceptions Date: Tue, 13 Aug 2019 17:02:07 +0200 Lines: 36 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit X-Trace: individual.net irevsvIUqORPADRTMTIzZwKJ29sGE6wCLJ41HIsUpy0PVyrzKd Cancel-Lock: sha1:6tXNZIXTgn2IAD+yCW7ugzAeiuc= User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:60.0) Gecko/20100101 Thunderbird/60.8.0 In-Reply-To: Content-Language: sv Xref: csiph.com comp.lang.c++:67623 On 2019-08-13 at 16:38, Jivanmukta wrote: > W dniu 13.08.2019 o 16:34, Jivanmukta pisze: >> I am learning C++ and I have a question: why: >> >> throw exception(); >> >> and not: >> >> throw new exception(); You can do either - throw an exception object or throw a pointer. In the latter case you have to catch a pointer and also remember to delete it somewhere. Extra work. And an extra allocation. So most people prefer the first version. > > Does it make sense to code: > > MyClass *myObj = MyClass(); > > instead of: > > MyClass *myObj = new MyClass(); No. You can just do MyClass myObj; and be done with it. No need for pointers here. Bo Persson