exception-handling

Thoughts on try-catch blocks

梦想与她 提交于 2019-12-10 04:47:16
问题 What are your thoughts on code that looks like this: public void doSomething() { try { // actual code goes here } catch (Exception ex) { throw; } } The problem I see is the actual error is not handled, just throwing the exception in a different place. I find it more difficult to debug because i don't get a line number where the actual problem is. So my question is why would this be good? ---- EDIT ---- From the answers it looks like most people are saying it's pointless to do this with no

How to handle ConcurrentModificationException in Android

前提是你 提交于 2019-12-10 03:57:41
问题 Im trying to delete item from a ArrayList. Some times it pops an exception, "java.util.ConcurrentModificationException". First i tried to remove them by array_list_name.remove(i), but it failed and some people were asked to use Iterator instead. So my current code is as follows. for (Iterator<Collectable> iter = array_list_name.iterator(); iter.hasNext();) { Collectable s = iter.next(); if (s.equals(array_list_name.get(id))){ iter.remove(); return true; } } And i call "array_list_name" inside

What type of Exception should I throw when an unknown value is passed into a switch statement

随声附和 提交于 2019-12-10 03:54:44
问题 Edit 1 Updated to make the enum not an argument to the method... Question This type of problem comes up a lot with enums in switch statements. In the example code, the developer has accounted for all countries the program is currently using, but if another country is added to the Country enum, an exception should be thrown. My question is, what type of exception should be thrown? Example Code: enum Country { UnitedStates, Mexico, } public string GetCallingCode(Guid countryId){ var country =

Emulating try-with-finally in OCaml

北城余情 提交于 2019-12-10 03:39:57
问题 OCaml's try .. with does not offer a finally clause like Java. It would be useful, though, especially when dealing with side effects. For example, I like to open a file, pass the open file to a function, and close it. In case the function raises an exception I have to catch it in order to have a chance to close the file. This gets increasingly complicated when multiple files are opened and opening itself might fail as well. Is there an established programming pattern to deal with this? Below

Visual Studio 2013 “break on handled exceptions” not working, not breaking

荒凉一梦 提交于 2019-12-10 03:36:47
问题 I am debugging an asp.net application on iisexpress.exe, I have configured visual studio 2013 to break on user-handled exceptions through the exception settings window but it still does not break when an exception is thrown. When I pause execution I can see on the Intellitrace window that a lot of exceptions were thrown but visual studio didn't break. 回答1: Ok, it seems it was because of the "Enable Just My Code" options was selected under Options->Debugging->General I don't know why but by

Should exception messages that are targeted to the developer be localized?

[亡魂溺海] 提交于 2019-12-10 03:36:33
问题 I am referring to exception messages that show the developer is incorrectly using an API. For example incorrectly passing a null to method. So the type of exception that the developer will get the first time they have run their incorrect code. The type of exception message that should never get to be displayed to the user of a system. This is kind of related to the theory that since the programming language is in English then the programmer already has an understanding of English. Or at least

Catch in Java a exception thrown in Scala - unreachable catch block

不羁岁月 提交于 2019-12-10 03:34:20
问题 Scala doesn't have checked exceptions. However, when calling scala code from java, it's desirable to catch exceptions thrown by scala. Scala: def f()= { //do something that throws SomeException } Java: try { f() } catch (SomeException e) {} javac doesn't like this, and complains that "this exception is never thrown from the try statement body" Is there a way to make scala declare that it throws a checked exception? 回答1: Use a throws annotation: @throws(classOf[SomeException]) def f()= { //do

catching exception from boost::filesystem::is_directory

好久不见. 提交于 2019-12-10 03:18:57
问题 I am currently catching errors from boost::filesystem::is_directory and showing the error to the user by calling "what()" on the exception. This gives the reason for failure but the error is strange to the user. For example: boost::filesystem::is_directory: Access is denied How can I catch the boost error and figure out what the actual cause is, so I can show a nicer error message? 回答1: By "nicer error message" would you mean something like #include <iostream> #include <boost/filesystem.hpp>

Powershell $Error object not immediately populating inside PSM1 module

佐手、 提交于 2019-12-10 03:16:58
问题 I'm encountering a peculiar issue with Powershell. I'm catching an Exception in a catch block, but the global $Error object is not being populated. A trivial example, where this would behave as expected is this: function Bar { Foo } function Foo { try { $Error.Clear() throw "Error!" } catch { "Caught an error - current error count $($Error.Count)" } finally { "Cleaning up - current error count $($Error.Count)" } } Output is as you would expect if you call Bar Caught an error - current error

Symfony 2.2 extend ExceptionController

試著忘記壹切 提交于 2019-12-10 03:12:32
问题 This question is related to the following change (part of Symfony 2.2 release): Part 1 In pre-2.2 Symfony, I was overriding ExceptionController to display some custom error pages. I did that via: parameters: twig.exception_listener.controller: My\CustomBundle\CustomExceptionController::showAction Now, after upgrading to 2.2, I can no longer do that, because an exception is thrown while generating an exception (no pun intended): ExceptionController::__construct() must be an instance of Twig