By "real" I mean Exceptions that would work as the ones predefined by the system.

I mean for a custom exception you have to use the reserved word throw CustomException once you have checked the condition that might cause the Exception, but if you have done that there's not that big o f a need to throw the Exception, you can work on what it's causing the exception on the same if block.

But as far as I know there's no way to do, for example in C# and in Java something like this:

Let's suppose a Custom Exception should be fired every time the user inputs a String that contains a "0", then in the Class that defines the Exception there should be a method that could be something like this:

void WayOfActingIfAConditionHappens(Condition thatCondition)

Then automatically whenever that Condition happens the Exception is thrown similar as predefined Exception work, as long as it's on a try, catch block.

I mean now instead of checking conditions and throwing the Exception you could work this way:

... (Code without throws MyException)
catch (MyException ex)

This way instead of having to check for every thing the user inputs, you could just let the try catch block do the job.

What are the reasons for not having something like that implemented? Would it be too difficult to do?

closed as unclear what you're asking by JacquesB, Thomas Junk, whatsisname, Useless, Ixrec May 8 '16 at 22:26

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • When would WayOfActingIfAConditionHappens() be called? – JacquesB May 8 '16 at 12:17
  • It wouldn't be explicitly called, but whenever the user would input something with a "0" it would be called, same way the program would call the method that treats a divideByZeroException if a division by zero happens. – user2638180 May 8 '16 at 12:20
  • 1
    The DivideByZeroException is thrown by the implementation of the division operation. In theory there is a linie which says if (dividend==0) throw new DivideByZeroException(). – JacquesB May 8 '16 at 12:28
  • The thing I think you're describing isn't really an exception. The pattern also violates the software engineering principles of encapsulation, the open/closed principle, and the Law of Demeter. – James Youngman May 11 '16 at 8:12

The predefined exception does not work as you describe. When a predefined exception is thrown, it is just because there is a throw-statement somewhere indside the framework library, which is called as part of the regular execution path.

If I understand you suggestion correctly, you propose that a exception class should be able to detect automatically when a condition arises anywhere in the program - without the check for the condition having to be called explicitly. In an imperative language this would mean the framework would have to automatically execute all possible exception checks after each operation it the program. This would be prohibitively expensive. Never mind it wouldn't be very useful in the first place.

  • I don't think it should neccesarily be that expensive, after each execution it could be enough with reading something, like the "Exception word" where it would be indicated if a Exception has happened, its type, its associated Message etc... That's how I thought predefined Exceptions happened, but I see it's not the case. – user2638180 May 8 '16 at 14:03
  • 2
    @user2638180 Let's say you define 500 of these magic exceptions that can be thrown from anywhere. You run the line i = i + 1 now 500 tests have to be run to see if those 500 exceptions apply (whether they would throw or not or even make sense). Best case scenario your code is 500 times slower – Richard Tingle May 8 '16 at 20:40

Not the answer you're looking for? Browse other questions tagged or ask your own question.