exception-handling

Java reflection and checked exceptions

拜拜、爱过 提交于 2019-12-05 11:25:06
I have a method which I would like to call via reflection. The method does some various checks on its arguments and can throw NullPointer and IllegalArgument exceptions. Calling the method via Reflection also can throw IllegalArgument and NullPointer exceptions which need to be caught. Is there a way to determine whether the exception is caused by the reflection Invoke method, or by the method itself? If the method itself threw an exception, then it would be wrapped in a InvocationTargetException . Your code could look like this try { method . invoke ( args ) ; } catch (

How to customize uncaught exception termination behavior?

泪湿孤枕 提交于 2019-12-05 11:19:04
In g++ and clang++ (in Linux at least) the following typical message is shown after an exception is thrown and not catch (uncaught exception): terminate called after throwing an instance of 'std::runtime_error' what(): Bye For example in: #include<stdexcept> int main(){ throw std::runtime_error("Bye"); } How do I customize the error message while still having full access to the thrown exception? The documentation ( http://www.cplusplus.com/reference/exception/set_unexpected/ ) mentions set_unexpected (and set_terminate ) but I don't know how the unexpected_handle has actual access to the

Throwing a new exception while throwing an old exception

久未见 提交于 2019-12-05 11:13:27
If a destructor throws in C++ during stack unwinding caused by an exception, the program terminates. (That's why destructors should never throw in C++.) Example: struct Foo { ~Foo() { throw 2; // whoops, already throwing 1 at this point, let's terminate! } }; int main() { Foo foo; throw 1; } terminate called after throwing an instance of 'int' This application has requested the Runtime to terminate it in an unusual way. Please contact the application's support team for more information. If a finally block is entered in Java because of an exception in the corresponding try block and that

Why does a Java Lambda which throws a Runtime Exception require brackets?

丶灬走出姿态 提交于 2019-12-05 11:05:11
I understand that a lambda in java cannot throw a checked exception, but can throw a RuntimeException, but why does the below code require brackets? Map<String, Integer> m = new HashMap<>(); Integer integer = m.computeIfAbsent("", s -> {throw new IllegalArgumentException("fail");}); Why can't you have? m.computeIfAbsent("", s -> throw new IllegalArgumentException("fail")); Is it due to the assumption of the compiler that it would return in this instance an int, so therefor can't have a return of an exception, even though its thrown? Sotirios Delimanolis The Java Language Specification

using exceptions for non-error purposes

风格不统一 提交于 2019-12-05 11:04:11
Is it a good practice to use exception for managing cases that are not errors ? Like in JavaScript and Python that manage the StopIteration case in generators (yield keyword). It depends on the language. Each language has it's own design and idioms. For most languages exceptions should be exceptional. In languages like C++, Java and C# it is very bad form to use exceptions for anything else. In Python, exceptions are used more frequently for things like the end of an iteration. There is much more of a model of try to do what you want and handle the exceptions later rather than validating input

How to handle free() errors in C?

天大地大妈咪最大 提交于 2019-12-05 10:52:56
Suppose that I have used a free() function to free a memory that,for many reasons, I'm not allowed to. How can I stop my C application from crashing and just generate an error and continue the execution? I don't have try-catch kind of provision here (like C++/java...). Is there any way to ignore this error and continue execution? If yes, How do you do that? More importantly, is it advisable to do so (continuing execution considering this memory error occurred)? Thank you There is nothing in the C standard that you can use to do what you want. The description of the free function is very clear

How many function calls will cause stack overflow

青春壹個敷衍的年華 提交于 2019-12-05 10:50:06
Hello android/Java developers, When a function call a function and that function call another one and so on, how many calls (stack length) would get me into stack over flow? Is there a general rule of thumb? The reason i am asking is because I am now which is more efficient(design wise) for my 5 players cards game Solution 1: for(int i=0;i<100;i++){ p1.play(); p2.play(); p3.play(); p4.play(); } Solution 2: p1.play(); //where p1.play() calls p2.play() and so on until p4 calls p1 again. // this will go on for 100 times I prefer solution 2 so if there is a crash I can see all the function calls

How to disable floating point unit (FPU)?

萝らか妹 提交于 2019-12-05 10:27:40
I want to disable FPU/MMX/SSE instructions in x86 system, and I will implement a handler for the Device-Not-Available exception. I have referred to Control register wiki page ; It seems that I have to set some flags in cr0 register. How to set these flags in cr0 and Do this work do at boot time? The Linux kernel code for managing FPU state can be found in arch/x86/kernel/traps.c , do_device_not_available() . By default, the Linux kernel disables the FPU for all processes, and enables it on first access. This allows the kernel to reduce context switch overhead for processes that don't use the

Visual Studio 2015 unexpectedly breaking on handled exceptions

谁说胖子不能爱 提交于 2019-12-05 10:10:36
An image being worth a lot of words, how is the following possible: As can be seen, Visual Studio 2015 (latest version) breaks while Common Language Runtime Exceptions under Exception Settings is unchecked, Enable Just My Code under Tools > Options > Debugging is checked, and the exception is clearly handled (within a try/catch block). The line failing and causing the break is a call to an external API (which is somewhat buggy, hence the try/catch block). Am I missing something that would justify the break or is this a bug? I thought this other question would provide some insight but it

Handling 404 in WCF Rest Service

自闭症网瘾萝莉.ら 提交于 2019-12-05 10:09:03
I have a wcf rest service on IIS 7.5. When someone visits a part of the endpoint that doesn't exist (i.e. http://localhost/rest.svc/DOESNOTEXIST vs http://localhost/EXISTS ) they are presented with a Generic WCF gray and blue error page with status code 404. However, I would like to return something like the following: <service-response> <error>The url requested does not exist</error> </service-response> I tried configuring the custom errors in IIS, but they only work if requesting a page outside of the rest service (i.e. http://localhost/DOESNOTEXIST ). Does anyone know how to do this? Edit