Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the point of an OurCompanyRuntimeException type in Java?

Tags:

java

exception

At the company where I am right now, there are a lot of places in the code where an 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 thrown by code that we wrote here at this company."

I'm somewhat new to Java, but I thought exception types were supposed to reflect what went wrong, not whose code threw the exception. For example, IllegalArgumentException means someone passed an illegal argument to something. You wouldn't have a SunIllegalArgumentException if an illegal argument was passed in code written by Sun, and then a IBMIllegalArgumentException -- that would be silly and pointless, right? And if you want to know where the exception was thrown, you look at the stack trace. I understand wanting to extend RuntimeException (so that you don't have as many try/catches or "throws"es all over) but why not make subclasses that explain what happened, as opposed to what company's code it happened in?

Has anyone ever used the OurCompanyRuntimeException idea before, or have an idea of why they might have done it this way?

like image 562
Tyler Avatar asked Dec 10 '22 20:12

Tyler


2 Answers

Sounds like the usual bespoke code nonsense that you find in in-house code bases to me. I suspect if you ask about there'll have been some decree that the OurCompanyRuntimeException was used based on some misguided logic by a senior guy that nobody dared question and has long since moved on - the story of the monkeys, bananas and hose-pipes springs to mind.

I agree with you, the name of the exception should be indicative of the error that has occurred.

like image 74
Nick Holt Avatar answered Feb 09 '23 00:02

Nick Holt


Helps when reading stack traces, that's for sure. I.e. when scanning through a lot of lines of 'caused by's', it helps to see that it occurred in something thrown by you, not something internal to, say, a container.

Also lets you perform custom actions as part of the throwable - e.g. write to a special log somewhere, etc.

like image 43
Alex Taylor Avatar answered Feb 09 '23 00:02

Alex Taylor