custom-exceptions

How to raise my exceptions instead of built-in exceptions?

主宰稳场 提交于 2021-01-27 19:07:46
问题 In some cases, I need to raise my exception because built-in exceptions are not fit to my programs. After I defined my exception, python raises both my exception and built-in exception, how to handle this situation? I want to only print mine? class MyExceptions(ValueError): """Custom exception.""" pass try: int(age) except ValueError: raise MyExceptions('age should be an integer, not str.') The output: Traceback (most recent call last): File "new.py", line 10, in <module> int(age) ValueError:

PostgreSQL custom exception conditions

旧巷老猫 提交于 2020-07-08 04:32:09
问题 Is it possible to create custom conditions when I raise an exception? Consider the following example: BEGIN y := x / 0; EXCEPTION WHEN division_by_zero THEN RAISE NOTICE 'caught division_by_zero'; RETURN x; END; Here I use 'division_by_zero' condition to catch the exception. What I'd like to do is something like this: BEGIN [...] RAISE custom_condition; EXCEPTION WHEN custom_condition THEN [...] END; so that I don't interfere with possible standard exceptions. I could just do y:= 1 / 0; and

Best practice of log custom exceptions in PHP

╄→гoц情女王★ 提交于 2020-01-24 06:42:40
问题 I have a custom exception (which is further extended in may other custom exceptions). My project requires log all the customExceptions (and all of its decendents) that occurs. I have a logger that can log customException (and anything else). One of the way of doing so is explicitly log the exception whenever it is being handled as follows. try{ //some exception occur } catch(customeException $e) { $log->logException($e); $e->showMessage(); // or do anything that we have to do with the error.

User defined exceptions: when do we use them? What is an “exceptional” situation?

左心房为你撑大大i 提交于 2020-01-11 09:15:08
问题 I'm sure that this question has been addressed in many best practices books, but still... Most of the times I see examples of wrong usage of custom exceptions, therefore I was wondering what would be a good situation to use them? In particular, at the moment I'm working on a type checker for a compilers course.Therefore, I have a SymbolTable class, which is rather similar to a Map.The key difference from your ordinary map is that each symbol has to be defined at most once, so a put(String,

Handing multiple exception of same type and resume execution in python

久未见 提交于 2020-01-02 06:35:54
问题 It appears that one cannot resume the execution of test once you hit the raise the user defined exception in Python. But in my scenario I want to check error occurred for different input values. But Current implementation is restricting to continue the check error for the different input vectors. However, before coming up with some sort of convoluted solution to my problem, I figured I would ask the experts and see if there is something I'm missing. Unfortunately, I cannot change this

Python: Maximum recursion depth exceeded when printing custom exception

亡梦爱人 提交于 2020-01-02 01:46:29
问题 The following code throws RuntimeError: maximum recursion depth exceeded while getting the str of an object . I can resolve the infinite recursion in two different ways, but I don't understand why each fix works and thus don't know which to use, or if either are correct. class FileError( Exception ): def __init__( self, filename=None, *a, **k ): #Fix 1: remove super super( FileError, self ).__init__( self, *a, **k ) self.filename = filename def __repr__( self ): return "<{0} ({1})>".format(

FaultException and custom exception WCF

≡放荡痞女 提交于 2019-12-29 07:55:11
问题 I have a question on how to send a custom exception as FaultException. It works when I use a system Exception like ArgumentException, but if I change it to my custom exception "TestException" it fails. I can’t get the configuration for the service reference, when I try to add it. Works: [OperationContract] [FaultContract(typeof(ArgumentException))] [TransportChannel TestMethod (); public Void TestMethod() { throw new FaultException<ArgumentException>(new ArgumentException("test"), new

How to create exceptions?

泪湿孤枕 提交于 2019-12-29 03:39:06
问题 So I have an upcoming assignment dealing with exceptions and using them in my current address book program that most of the homework is centered around. I decided to play around with exceptions and the whole try catch thing, and using a class design, which is what I will eventually have to do for my assignment in a couple of weeks. I have working code that check the exception just fine, but what I want to know, is if there is a way to standardize my error message function, (i.e my what() call

How to catch a pywin32com exception on opening files

主宰稳场 提交于 2019-12-25 08:57:40
问题 I am trying to open an excel file in python using COM, and trying to catch the file not found error: I first tried catching the IOError: try: output = xl.Workbooks.Open(Params.workbookName) except IOError as reason: print reason exit() But COM doesn't raise an IO Error when it has a file not found problem, instead it raises something called com_error: com_error: (-2147352567, 'Exception occurred.', (0, u'Microsoft Office Excel', u"'asdf.xlsx' could not be found. Check the spelling of the file