Questions tagged [custom-exceptions]

Custom Exception inherits the properties from the Exception class. Whenever we declare our own exception, it is known as custom exception.

Custom Exception inherits the properties from the Exception class. Whenever we declare our own exception, it is known as custom exception.

242 questions
101
votes
4 answers

Ruby custom error classes: inheritance of the message attribute

I can't seem to find much information about custom exception classes. What I do know You can declare your custom error class and let it inherit from StandardError, so it can be rescued: class MyCustomError < StandardError end This allows you to…
MarioDS
  • 12,895
  • 15
  • 65
  • 121
86
votes
5 answers

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

Is it possible to create user-defined exceptions and be able to change the SQLERRM? For example: DECLARE ex_custom EXCEPTION; BEGIN RAISE ex_custom; EXCEPTION WHEN ex_custom THEN DBMS_OUTPUT.PUT_LINE(SQLERRM); END; / The…
tgxiii
  • 1,355
  • 1
  • 15
  • 19
85
votes
4 answers

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

What are industry standard best practices for implementing custom exceptions in C#? I have checked Google and there's a great number of recommendations, however I don't know which ones hold more credibility. If anybody has any links to…
Darren Young
  • 10,972
  • 36
  • 91
  • 150
72
votes
8 answers

Catch Multiple Custom Exceptions? - C++

I'm a student in my first C++ programming class, and I'm working on a project where we have to create multiple custom exception classes, and then in one of our event handlers, use a try/catch block to handle them appropriately. My question is: How…
Alex
  • 64,178
  • 48
  • 151
  • 180
52
votes
2 answers

Custom Python Exceptions with Error Codes and Error Messages

class AppError(Exception): pass class MissingInputError(AppError): pass class ValidationError(AppError): pass ... def validate(self): """ Validate Input and save it """ params = self.__params if 'key' in params: …
Sam
  • 1,358
  • 4
  • 16
  • 30
50
votes
8 answers

C#: Throwing Custom Exception Best Practices

I have read a few of the other questions regarding C# Exception Handling Practices but none seem to ask what I am looking for. If I implement my own custom Exception for a particular class or set of classes. Should all errors that relate to those…
user295190
43
votes
1 answer

Using mockito to test methods which throw uncaught custom exceptions

How do I write a Mockito-based JUnit method to test this method adduser()? I tried writing one, but it's failing with an error message saying exception is not handled. The error is displayed…
Sekhar
  • 961
  • 5
  • 15
  • 27
29
votes
5 answers

Wrap jQuery's $.ajax() method to define global error handling

Branching off of questions like this one, I'm looking to wrap jQuery's $.ajax() method such that I can provide error handling in one location, which would then be used automatically by all of an application's remote calls. The simple approach would…
Marc L
  • 2,064
  • 1
  • 19
  • 12
28
votes
4 answers

Can you throw an array instead of a string as an exception in php?

I want to throw an array as an exception in php, instead of a string. Is it possible to do this if you define your own class that extends the Exception class? For example throw new CustomException('string', $options = array('params'));
john mossel
  • 2,158
  • 5
  • 24
  • 39
24
votes
2 answers

Throwing custom exceptions in Javascript. Which style to use?

Douglas Crockford recommends doing something like this: throw { name: "System Error", message: "Something horrible happened." }; But you could also do something like this: function IllegalArgumentException(message) { this.message =…
Vivin Paliath
  • 94,126
  • 40
  • 223
  • 295
22
votes
3 answers

Getting a list of existing Rails Error classes for re-use/inheritance

Often I need to throw a custom(ized) error. Like when a resource cannot be found due to a mismatch in parameters or so. I prefer to throw existing errors or, throw an error that is inherited from an existing error. That way, I don't introduce Error…
berkes
  • 26,996
  • 27
  • 115
  • 206
21
votes
3 answers

Exception handler in Spring MVC

I want to create an exception handler which will intercept all controllers in my project. Is that possible to do? Looks like I have to put a handler method in each controller. Thanks for your help. I have a spring controller that sends Json…
fastcodejava
  • 39,895
  • 28
  • 133
  • 186
21
votes
4 answers

How to create exceptions?

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…
Charlie M
  • 273
  • 1
  • 5
  • 11
19
votes
3 answers

What is the Python equivalent of Java's UnsupportedOperationException?

I'm looking at Python's built-in exceptions and wondering what the closest equivalent of Java's UnsupportedOperationException is. NotImplementedError is close but seems to suggest something slightly different. Should I be using RuntimeError or…
erwaman
  • 3,307
  • 3
  • 28
  • 29
17
votes
3 answers

C++ Exceptions with message

I'm not sure that my custom exception approach is correct. What I want to do is to throw exceptions with custom messages but it seems that I created a memory leak... class LoadException: public std::exception { private: const char*…
Alex Netkachov
  • 13,172
  • 6
  • 53
  • 85
1
2 3
16 17