What is a point like OurCompanyRuntimeException in Java?

In the company where I am right now, there are many places in the code where OurCompanyRuntimeException is thrown (where OurCompany is the actual name of the company). As far as I can tell, this exception is described as a "runtime exception created by code written here in this company."

I'm a little new to Java, but I thought that the types of exceptions should reflect what went wrong and not whose code generated the exception. For example, IllegalArgumentException means that someone passed some illegal argument. You would not have a SunIllegalArgumentException exception if an illegal argument were passed in code written by Sun and then an IBMIllegalArgumentException exception - that would be stupid and pointless, wouldn't it? And if you want to know where the exception was thrown, you are looking at the stack trace. I understand that I want to extend RuntimeException (so that you do not have so many attempts / catches or “throws”), but why not make subclasses explaining what happened, as opposed to in which company code it happened?

Has anyone ever used the idea of ​​OurCompanyRuntimeException, or do you have an idea why they could do it this way?

+4
source share
8 answers

It looks like ordinary custom-made nonsense that you find in the internal codes for me. I suspect that if you ask that there will be some kind of decree that OurCompanyRuntimeException was used on the basis of the erroneous logic of an older guy that no one dared to doubt and had long passed on - a story about monkeys, bananas and hose pipes come to the head.

I agree with you, the name of the exception should indicate an error that has occurred.

+6
source

Helps in reading stack traces, that's for sure. That is, when scanning through many lines of “triggered”, it helps to see that this happened in something that you chose, and not something internal, say, in the container.

It also allows you to perform custom actions as part of the casts - for example, write to a special journal somewhere, etc.

+2
source

Yes, I also found it, but that also made no sense. I assumed that companies wrote these exceptions very early after the adoption of Java, without getting a correct idea of ​​how the throw and handling of exceptional situations really work (for example, Nick already said ... as a senior programmer whom no one dared to ask). If a company considers it necessary to create its own class of exceptions (for example, for specific companies involved in logging), this exception should never be thrown directly (making it abstract). I would get a specific task describing exceptions or just following the Spring Framework's idea for handling / throwing exceptions.

+2
source

This is a bad concept. Exceptions must be specific to use.

Well, if the company really produces a lot of wrong codes / products, they can use this type of exception as an advertisement;)

+1
source

Your company can add code to an existing project, for example, an open source database, and could simply add very small code to it. Therefore, in order to keep track of the mistakes that the developers of the company were making, they thought that they would have their own class of exceptions to distinguish the errors that were there before from those caused by the extension. Thus, they can only focus on those that are called by the company’s developers, and perhaps ask the original source code developers to support them.

Over time, when you developed a sufficiently large code base through internal development, you can add more exceptions and remove CompanynameRuntimeException altogother. In addition, they can get more pleasure from the level of expert knowledge of the developers, to allow them to handle all the errors as one, and not to view the suspicions caused by the developers of the company.

+1
source

It would be very helpful to have this as a base class for certain exceptions. You have chosen a specific exception and caught the base class.

It may also lead to a cause (exception REAL) plus additional information. This can be very convenient for creating diagnostic output for logging.

+1
source

It seems pretty dumb, log output or stack trace will show you who is violating the class, so the explanation is not erased. It also seems dangerous, as if people are encouraged to throw OurCompanyRuntimeException, which they throw RuntimeExceptions that do not force the caller to process them and can delete your application.

I agree with you that exceptions should reflect the cause of their occurrence. I saw a custom exception as the root of the hierarchy, although it probably should be abstract, so you need to create a specific extension to use it, and this definitely should not be a RuntimeException exception.

+1
source

It would be nice to have a common class of exceptions for common companies, as you describe, which inherits more specific cases of exception. One answer already mentioned that the ability specifically catches internal code exceptions and ignores / skips them from the main Java code or a third-party library. The key point here is that more specific exceptions should inherit from this. Throwing a typical exception named by the company will rarely be required and is almost never recommended.

0
source

All Articles