exception-handling

Why use an exception instead of if…else

浪子不回头ぞ 提交于 2019-12-10 19:29:10
问题 For example, in the case of "The array index out of bound" exception, why don't we check the array length in advance: if(array.length < countNum) { //logic } else { //replace using exception } My question is, why choose to use an exception? and when to use an exception, instead of if-else Thanks. 回答1: They are used to inform the code that calls your code an exceptional condition occurred. Exceptions are more expensive than well formed if/else logic so you use them in exceptional circumstances

Need a method for searching all of C drive for a directory in C#

我怕爱的太早我们不能终老 提交于 2019-12-10 19:17:58
问题 I need to find a folder called "GameData", in the filesystem, its normally stored in Program Files, but if it is not, i would like to be able to find it, wherever it is in the C drive. Currently, i'm using: IEnumerable<string> list = Directory.GetDirectories(root, "GameData", SearchOption.AllDirectories); But it throws an UnauthorizedAccessException which is expected, as it is trying to navigate through protected directories. Is there perhaps something i can add to this to stop it from

How to add throws Exception clause when implementing a method defined in a interface without throws clause?

空扰寡人 提交于 2019-12-10 19:12:47
问题 I need a class to navigate across a collection, then I implemented Iterator interface. But the problem is, my implementation of next() method need to throw an Exception, because the collection members need to be generated dynamically, and Exceptions may happen during the generating process. The only exception throws by next() is NoSuchElementException which means no more element in the collection, and this does not meet my need. Or, I should not implement Iterator at all? 回答1: To be pedantic,

How can I get the standard file streams to return a useful error message?

给你一囗甜甜゛ 提交于 2019-12-10 18:49:44
问题 #include <fstream> #include <iostream> #include <exception> int main(int argc, char **argv) { try { std::ifstream sFile(argv[1]); sFile.exceptions(std::ios::badbit | std::ios::failbit); } catch (const std::exception &_r) { std::cerr << "exception: " << _r.what() << std::endl; } } In case of the file passed in does not exist, this code prints out with g++ 4.5.2 (yes I know that this is a very old version, but I don't have enough clout to change this): "exception: basic_ios::clear" Using Visual

Initialize exception with stacktrace from another exception?

﹥>﹥吖頭↗ 提交于 2019-12-10 18:27:34
问题 I have client-server system. They communicate via RMI, so serialization/deserialization is involved. Server sends a response to client upon a request. If exception occurs it is set in the response. However, if some exception occurs at the server and the client does not know about it. So I need to wrap the original exception but to keep the stacktrace for debug purposes. Is there more elegant solution? //response from server to client class Response { private MyException e; public void set

How to try/catch all exceptions

回眸只為那壹抹淺笑 提交于 2019-12-10 18:26:09
问题 I'm completing a UWP app started by someone else. The app crashes frequently and I always end up in App.g.i.cs at if (global::System.Diagnostics.Debugger.IsAttached) global::System.Diagnostics.Debugger.Break(); where I then have to say "no, don't start the debugger" and close 2 windows. Is there somewhere I could put a big try/catch so that I don't have to restart the app each time this happen? I can't find anything in AppShell or App . Or do I have to put a try/catch in every single event

C++ catch enum value as exception

邮差的信 提交于 2019-12-10 17:56:42
问题 I am trying to use an external C++ library which have defined its exceptions as: enum MY_ERRORS { ERR_NONE = 0, ERR_T1, ERR_T2, }; Then in the code exceptions are thrown like this: if(...) { throw ERR_T1; Being new to programming in C++, I would do something like: try { call_to_external_library(); } catch(??? err) { printf("An error occurred: %s\n", err); } catch(...) { printf("An unexpected exception occurred.\n"); } How do I determine what was thrown? 回答1: You will need to write your code

How can I catch a NoSuchMethodException?

让人想犯罪 __ 提交于 2019-12-10 17:37:48
问题 There are some incompatible changes in code which I depend on. So I want to catch a NoSuchMethodException to log more information about the problem When I'm using this: try{ do.something(); }catch(NoSuchMethodException e){ System.out.println("!"); } I get this error "Unreachable catch block for NoSuchMethodException. This exception is never thrown from the try statement body" I've tried to catch also java.lang.RuntimeException and check if it's NoSuchMethod but it didn't work. Reflection will

WPF DataBinding watch for thrown exceptions

北城以北 提交于 2019-12-10 17:13:47
问题 In my model I have lots of properties for different objects and I'm checking the value while setting value for object and if the value is not accepted I will throw an exception this was working perfect with windows forms propertygrid but now I'm trying to design a new interface using WPF . in WPF when I bound a property to a control like textbox ,when the value is changed I don't know how to handle the exception and show the error message . example : public string ConnectionString { get {

asp.net mvc errorhandler not showing custom error page

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-10 17:09:10
问题 I am trying to follow the examples in this link and this one but instead of showing the error page I get an HTTP 500 Internal server error. I have the <customErrors mode="On" /> set in the webconfig. I have even tried applying the [HandleError] filter to the controller class. I tried without as well. The Error.aspx is present in /Views/Shared/ as well so it couldn't be a case of no file found. I threw a DivideByZero exception in my controller's action method. I want to follow that example so