throw

Why do I always get “terminate called after throwing an instance of…” when throwing in my destructor?

偶尔善良 提交于 2019-12-07 03:46:11
问题 I'm trying to write a unit test that detects an invalid use of the lock() feature of my class. In order to do so, I want to use the destructor and throw an exception from there. Unfortunately, instead of catching the exception, g++ decides to call std::terminate(). There is a very simplified version of the class: class A { public: A() : f_lock(0) {} ~A() { if(f_lock) throw my_exception("still locked"); } lock() { ++f_lock; } unlock() { --f_lock; } private: int f_lock; }; There is a valid test

Best way to handle errors from async closures in Swift 2?

廉价感情. 提交于 2019-12-07 03:14:54
问题 I'm using a lot of async network request (btw any network request in iOS need to by async) and I'm finding way to better handle errors from Apple's dataTaskWithRequest which not supports throws . I have code like that: func sendRequest(someData: MyCustomClass?, completion: (response: NSData?) -> ()) { let request = NSURLRequest(URL: NSURL(string: "http://google.com")!) if someData == nil { // throw my custom error } let task = NSURLSession.sharedSession().dataTaskWithRequest(request) { data,

Why is my mocha/chai Error throwing test failing?

爱⌒轻易说出口 提交于 2019-12-07 03:12:06
问题 I have a simple javascript package I'm trying to test. I want to check for an Error being thrown, but when my test is run, and the error is thrown, the test is marked as failing. Here's the code: var should = require('chai').should(), expect = require('chai').expect(); describe('#myTestSuite', function () { it ('should check for TypeErrors', function () { // Pulled straight from the 'throw' section of // http://chaijs.com/api/bdd/ var err = new ReferenceError('This is a bad function.'); var

Catch/Modify (Message)/Rethrow Exception of same type

蹲街弑〆低调 提交于 2019-12-07 01:47:02
问题 I want a central place to extract information from an exception, set all the information I need to its message parameter and then rethrow that information as an Exception of the same type. The better solution probably would be to do this at the place where the exception is finally being handled (and its message logged), but.. I have control over the place throwing the exception, and not over the place that receives the exception and only logs its Message content. Apart from that design

Lifetime of a thrown object caught by reference

陌路散爱 提交于 2019-12-06 07:13:58
问题 The C++ Standard, paragraph 15.1.4 sais: The memory for the temporary copy of the exception being thrown is allocated in an unspecified way, except as noted in 3.7.3.1. The temporary persists as long as there is a handler being executed for that exception. I'm wondering why this code crashes (I know that it's not best practice): class magicException { private: char* m_message; public: magicException(const char* message) { m_message = new char[strlen(message) + 1]; strcpy(m_message, message);

How do I throw a custom try-catch exception on CakePHP?

旧街凉风 提交于 2019-12-06 04:22:19
I want to throw a custom exception which is the data validation exception in a controller of my CakePHP application. How do I create my own custom exception handler in Cakephp so that I can throw the exception and catch the exception? My code example: function getUserDetails($userid){ try{ if(!$validUser){ throw new Exception('Invalid User'); } return $userDetailsData; //returned from db }catch(Exception $e){ echo 'Error:'.$e->getMessage(); return; } } is it possible to use here custom Exception class in cakephp so that only those exception can be thrown that what i do. Hope it clarifies

How can an array work with the conditional operator?

回眸只為那壹抹淺笑 提交于 2019-12-06 02:43:35
This is a retelling of my previous post , since I changed the question (so it probably didn't get flagged as a new question and was missed). I'll hopefully trim it down too. I had functions like: #include <cstddef> #include <type_traits> template < typename E, typename T > inline constexpr auto checked_slice( E &&, T &&t ) noexcept -> T && { return static_cast<T &&>(t); } template < typename E, typename T, std::size_t N, typename U, typename ...V > inline constexpr auto checked_slice( E &&e, T (&t)[N], U &&u, V &&...v ) -> typename remove_some_extents<T, sizeof...(V)>::type & { typedef

An exception throw has type `Nothing`?

馋奶兔 提交于 2019-12-06 02:30:17
As stated here - Chapter 7 of Programming in Scala, Built-in Control Structures, 7.4 Exception handling with try expressions : In Scala, throw is an expression that has a result type. Technically, an exception throw has type Nothing . You can use a throw as an expression even though it will never actually evaluate to anything. This little bit of technical gymnastics might sound weird, but is frequently useful in cases like the previous example. One branch of an if computes a value, while the other throws an exception and computes Nothing . The type of the whole if expression is then the type

ruby catch-throw and efficiency

安稳与你 提交于 2019-12-06 02:09:21
问题 catch in Ruby is meant to jump out of deeply nested code. In Java e.g. it is possible to achieve the same with Java's try-catch meant for handling exceptions, it is however considered poor solution and is also very inefficient. In Ruby for handling exceptions we have begin-raise-rescue and I assume it is also to expensive to use it for other tasks. Is Ruby's catch-throw really a more efficient solution then begin-raise-rescue or are there any other reasons to use it to break nested blocks

Declaration of void abort() throws different exceptions

こ雲淡風輕ζ 提交于 2019-12-05 20:34:53
I am trying to write some C++ code (using the C++ API) for Festival and am getting stuck while trying to compile. Here is how I invoke g++ : g++ -Wall -pedantic -I../ -I../speech_tools/include/ helloFestival.C -o h -L../festival/src/lib/libFestival.a -L../speech_tools/lib/libestools.a -L../speech_tools/lib/libestbase.a -L../speech_tools/lib/libeststrings.a |& tee festival.runLog The error I get is: In file included from ../speech_tools/include/EST.h:48, from ../festival/src/include/festival.h:47, from helloFestival.C:4: ../speech_tools/include/EST_String.h:50: error: declaration of ‘void abort