Custom Python Exceptions with Error Codes and Error Messages

Here’s a quick example of a custom Exception class with special codes: class ErrorWithCode(Exception): def __init__(self, code): self.code = code def __str__(self): return repr(self.code) try: raise ErrorWithCode(1000) except ErrorWithCode as e: print(“Received error with code:”, e.code) Since you were asking about how to use args here’s an additional example… class ErrorWithArgs(Exception): def __init__(self, *args): # … Read more

What are industry standard best practices for implementing custom exceptions in C#?

The standard for creating custom exceptions is to derive from Exception. You can then introduce your own properties/methods and overloaded constructors (if applicable). Here is a basic example of a custom ConnectionFailedException which takes in an extra parameter which is specific to the type of exception. [Serializable] public class ConnectionFailedException : Exception { public ConnectionFailedException(string … Read more

Oracle PL/SQL – Raise User-Defined Exception With Custom SQLERRM

Yes. You just have to use the RAISE_APPLICATION_ERROR function. If you also want to name your exception, you’ll need to use the EXCEPTION_INIT pragma in order to associate the error number to the named exception. Something like SQL> ed Wrote file afiedt.buf 1 declare 2 ex_custom EXCEPTION; 3 PRAGMA EXCEPTION_INIT( ex_custom, -20001 ); 4 begin … Read more

Ruby custom error classes: inheritance of the message attribute

raise already sets the message so you don’t have to pass it to the constructor: class MyCustomError < StandardError attr_reader :object def initialize(object) @object = object end end begin raise MyCustomError.new(“an object”), “a message” rescue MyCustomError => e puts e.message # => “a message” puts e.object # => “an object” end I’ve replaced rescue Exception … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)