exception-handling

std::unique_ptr::reset and constructor exceptions

拥有回忆 提交于 2019-12-23 07:48:45
问题 If initialise a unique_ptr like this: std::unique_ptr<Foo> i; i.reset( new Foo() ); but an exception is thrown from Foo::Foo() , the question is: what happens with the memory allocated? how does unique_ptr avoid it being leaked? is this something handled inside new operator? The destructor will certainly be called when the scope exits. Since the reset call is not invoked until new Foo() returns, it seems that this must be handled by new , by freeing the allocated memory when the exception

std::unique_ptr::reset and constructor exceptions

限于喜欢 提交于 2019-12-23 07:48:19
问题 If initialise a unique_ptr like this: std::unique_ptr<Foo> i; i.reset( new Foo() ); but an exception is thrown from Foo::Foo() , the question is: what happens with the memory allocated? how does unique_ptr avoid it being leaked? is this something handled inside new operator? The destructor will certainly be called when the scope exits. Since the reset call is not invoked until new Foo() returns, it seems that this must be handled by new , by freeing the allocated memory when the exception

how to catch an int

核能气质少年 提交于 2019-12-23 07:47:17
问题 I am using IL to throw an Int32 and catch it. This is just out of curiosity, I am not trying to achieve anything, so please dont tell me to throw an Exception instead of int. .method private hidebysig static void Main(string[] args) cil managed { .entrypoint // Code size 40 (0x28) .maxstack 2 .locals init (object V_0, int32 V_1) IL_0000: nop .try { IL_0001: nop IL_0002: ldsfld int32 ConsoleApplication3.Program::i IL_0007: throw } // end .try catch [mscorlib]System.Object { IL_0008: stloc.0 IL

Are try/catch for every single statement that throws an exception considered an anti-pattern?

随声附和 提交于 2019-12-23 07:42:04
问题 I am currently reviewing a colleagues Java code, and I see a lot of cases where every single statement that may throw an exception being encapsulated in its own try/catch. Where the catch block all perform the same operation (which operation is not relevant for my question). To me this seems like a code smell, and I do remember reading about it being a common anti-pattern. However I cannot find any references on this. So are try/catch for every single statement that throws and exception

How do I catch ClassCastException?

不想你离开。 提交于 2019-12-23 07:41:27
问题 I'm trying to catch a ClassCastException when deserializing an object from xml. So, try { restoredItem = (T) decoder.readObject(); } catch (ClassCastException e){ //don't need to crash at this point, //just let the user know that a wrong file has been passed. } And yet this won't as the exception doesn't get caught. What would you suggest? 回答1: The code in the question should give you an unchecked cast warning. Listen to -Xlint. All the compiler knows about T is its bounds, which it probably

PHP 5.5 and try … finally

时光毁灭记忆、已成空白 提交于 2019-12-23 07:29:25
问题 PHP 5.5 is adding support for finally in try/catch blocks. Java allows you to create a try/catch/finally block with no catch block, so you can cleanup locally when an exception happens, but let the exception itself propagate up the call stack so it can be dealt with separately. try { // Do something that might throw an exception here } finally { // Do cleanup and let the exception propagate } In current versions of PHP you can achieve something that can do cleanup on an exception and let it

Testing in Python - how to use assertRaises in testing using unittest? [duplicate]

让人想犯罪 __ 提交于 2019-12-23 07:03:18
问题 This question already has answers here : How do you test that a Python function throws an exception? (13 answers) Closed last year . I am trying to do a simple test in Python using unittest, to see if a class throws an exception if it gets an unsuitable input for the constructor. The class looks like this: class SummaryFormula: def __init__( self, summaryFormula): self.atoms = {} for atom in re.finditer( "([A-Z][a-z]{0,2})(\d*)", summaryFormula): symbol = atom.group(1) count = atom.group(2)

Custom exceptions in dart

别说谁变了你拦得住时间么 提交于 2019-12-23 06:49:52
问题 I have written this code to test how custom exceptions are working in dart. I'm not getting the desired output could someone explain me how to handle it?? void main() { try { throwException(); } on customException { print("custom exception is been obtained"); } } throwException() { throw new customException('This is my first custom exception'); } 回答1: You can look at the Exception part of A Tour of the Dart Language. The following code works as expected ( custom exception is been obtained is

Why the manual raised Transient Error exceptions are handled as an AggregateException?

假装没事ソ 提交于 2019-12-23 05:14:15
问题 When I try to raise transient exception manually, it is always handled as AggregateException . Since it is handled as AggregateException , it is not handled as transient error in my retry policy and not retried for the predefined retry count. Transient errors are shown here . Therefore I have tried CommunicationException and ServerErrorException but it is handled as an AggregateException. When I look for AggregateException, it says "Represents one or more errors that occur during application

Manage the errors of a framework

允我心安 提交于 2019-12-23 05:14:12
问题 I'm trying to develop a personal MVC framework for learning purposes. But every time I'm stuck in this problem: errors. I feel like I'm handling them very bad. Currently I have an exception system (everything is converted to exception, even PHP triggered errors) that is catch in a try{} block that contains every line of code of the framework and the user application. I'm treating errors such as "controller not found" or "action not found" like any other, for example "unable to connect to the