exception-handling

Throwing an UnsupportedOperationException

佐手、 提交于 2019-12-09 16:39:39
问题 So one of the method descriptions goes as follows: public BasicLinkedList addToFront(T data) This operation is invalid for a sorted list. An UnsupportedOperationException will be generated using the message "Invalid operation for sorted list." My code goes something like this: public BasicLinkedList<T> addToFront(T data) { try { throw new UnsupportedOperationException("Invalid operation for sorted list."); } catch (java.lang.UnsupportedOperationException e) { System.out.println("Invalid

FaultException.Detail coming back empty

一个人想着一个人 提交于 2019-12-09 16:28:53
问题 I am trying to catch a given FaultException on a WCF client. I basically need to extract a inner description from the fault class so that I can then package it in another exception for the upper layers to do whatever. I've done this successfully a number of time, what makes it different this time is that fault is declared as an array, as you can see from the service reference attribute declared on top of the method that throws the exception: [System.ServiceModel.FaultContractAttribute(typeof

How to simulate throwing an exception in Unit tests?

谁说我不能喝 提交于 2019-12-09 15:53:22
问题 How can I simulate an exception being thrown in C# unit tests? I want to be able to have 100% coverage of my code, but I can't test the code with exceptions that may occur. For example I cannot simulate a power faluire that may occur. For example: public void MyMethod() { try { ... } catch(OutOfMemoryException e) { ... } catch(RandomErrorFromDatabaseLayer e) { ... } } I want to be able to simulate any kind of exception that is in this method and should be caught. Are there any libraries that

Python try/except: trying multiple options

北城以北 提交于 2019-12-09 15:41:52
问题 I'm trying to scrape some information from webpages that are inconsistent about where the info is located. I've got code to handle each of several possibilities; what I want is to try them in sequence, then if none of them work I'd like to fail gracefully and move on. That is, in psuedo-code: try: info = look_in_first_place() otherwise try: info = look in_second_place() otherwise try: info = look_in_third_place() except AttributeError: info = "Info not found" I could do this with nested try

Sonar complaining about logging and rethrowing the exception

落爺英雄遲暮 提交于 2019-12-09 14:45:39
问题 I have the following piece of code in my program and I am running SonarQube 5 for code quality check on it after integrating it with Maven. However, Sonar is complaining that I should Either log or rethrow this exception . What am I missing here? Am I not already logging the exception? private boolean authenticate(User user) { boolean validUser = false; int validUserCount = 0; try { DataSource dataSource = (DataSource) getServletContext().getAttribute("dataSource"); validUserCount = new

python how to safely handle an exception inside a context manager

心不动则不痛 提交于 2019-12-09 14:19:43
问题 I think I've read that exceptions inside a with do not allow __exit__ to be call correctly. If I am wrong on this note, pardon my ignorance. So I have some pseudo code here, my goal is to use a lock context that upon __enter__ logs a start datetime and returns a lock id, and upon __exit__ records an end datetime and releases the lock: def main(): raise Exception with cron.lock() as lockid: print('Got lock: %i' % lockid) main() How can I still raise errors in addition to existing the context

Testing Exception Messages with Shouldly

核能气质少年 提交于 2019-12-09 14:02:19
问题 Is there a way to test the exception messages with shouldly? An example: public class MyException: Exception{ } The method to be tested: public class ClassUnderTest { public void DoSomething() { throw new MyException("Message"); } } I would usually test this in this way: [TestMethod] public void Test() { try { new ClassUnderTest().DoSomething(); Assert.Fail("Exception not thrown"); } catch(MyException me) { Assert.AreEqual("Message", me.Message); }catch(Exception e) Assert.Fail("Wrong

How to intercept WCF faults and return custom response instead?

a 夏天 提交于 2019-12-09 12:54:19
问题 Consider the following very basic WCF service implementation: public enum TransactionStatus { Success = 0, Error = 1 } public class TransactionResponse { public TransactionStatus Status { get; set; } public string Message { get; set; } } [ServiceContract] [XmlSerializerFormat] public interface ITestService { [OperationContract] TransactionResponse DoSomething(string data); } public class TestService : ITestService { public TransactionResponse DoSomething(string data) { var result =

Automatically Logging Exceptions in Ruby

大城市里の小女人 提交于 2019-12-09 12:09:23
问题 Is there a library or easy way to catch exceptions thrown in a Ruby program and log it to a file? I've looked over log4r and logger, but the docs on both don't provide any examples on how I would do this. I run this program remotely and lose handles to stdout and stderr, if that information helps at all. What would you recommend? 回答1: If you want to take a walk on the wild side, try this: class Exception alias real_init initialize def initialize(*args) real_init *args # log the error (self)

How can you catch a custom exception from Celery worker, or stop it being prefixed with `celery.backends.base`?

ⅰ亾dé卋堺 提交于 2019-12-09 10:53:33
问题 My Celery task raises a custom exception NonTransientProcessingError , which is then caught by AsyncResult.get() . Tasks.py: class NonTransientProcessingError(Exception): pass @shared_task() def throw_exception(): raise NonTransientProcessingError('Error raised by POC model for test purposes') In the Python console: from my_app.tasks import * r = throw_exception.apply_async() try: r.get() except NonTransientProcessingError as e: print('caught NonTrans in type specific except clause') But my