What are checked exceptions?

Asked

Viewed 519 times

5

While following a discussion about programming languages, I saw people arguing about checked excpetion each with its arguments for or against.

They said that such language implements and such language does not implement. My doubt is what are these checked exceptions?

Are special exceptions of some specific language?

2 answers

5


Checked Exception is a mechanism that tries, in theory, to give more robustness to applications forcing an exception to be treated one way or another by the next level in the call stack.

Many people consider that exception to be an interesting mechanism because it forces you to treat. You have no idea how many experienced people say this because one day someone said it and then the person repeats like a parrot without knowing what they’re talking about. It’s the exact opposite, it has a number of mechanisms that you have to deal with, but people only look at one that was very popular and doesn’t oblige. Exception also does not require, everything "works" or break without intervention of the programmer. It is part of the philosophy of the exception not to oblige treatment, leave it to another level to treat until it comes to a point that no one has dealt with and breaks everything. See more in Why should we avoid returning error codes?.

The exceptions verified came to change this, only they in fact require a treatment at the first level. Every method that throws an exception and has a signing, therefore part of the contract, a marked exception, so whoever calls this method will be obliged to treat the exception in some way. The most common is to put one try-catch, but it is possible to put in his signature that he casts this exception. At this point you no longer need to treat the exception right there, because he delegated and forced another method (that called him) to do this. So it’s not that you need to treat the exception in a useful way, you can treat it in a way to delegate it to another. It’s still a treatment, even without doing something useful.

Just that people get tired of dealing with it and start using a lot of try-catch empty just to satisfy the code. It’s wrong, of course, but people do a lot, even because there is a situation that the code does not scale well with so many verified exceptions. One of the reasons that new methods written even by Java chooses not to use such exceptions so much that it should give more robustness. Not only did Java adopt them (at least between languages mainstream). Everyone saw after it was a mistake. It was wanting to make perfect language and it went very wrong. If language helps, the programmer will do wrong to program the way he wants.

Therefore I question even the creation of exception, because almost everyone uses wrong. Not that I think something shouldn’t be put into a language because people can use it wrong, I’m against that philosophy (which Java loves so much, but only when it is in her interest, for example they do not want operator overload where I have never seen in real code someone abusing, but exception that everyone abuses all the time, may have).

It’s right that the staff did not whirl, but a cool thing of it is that it shows(ria) to the person if that exception is programming error and should not be treated (no signature obliging) or is application error at that time and should be treated (is a checked Exception). Too bad you misguided everything and stopped distinguishing that right, now it’s no longer reliable. And worse, Java considers too much programming error as a Runtime.

A method with a checked Exception it would be something like that:

public void teste() throws AlgumaException {
    //faz alguma coisa
    if (condicao) throw new AlgumaException();
    //faz alguma coisa
}

Then whoever calls has to do something like this:

public void teste2() throws AlgumaException {
    //faz alguma coisa
    teste();
    //faz alguma coisa
}

Or:

public void teste() {
    try {
        //faz alguma coisa
    } catch (AlgumaException ex) {
        //faz alguma coisa
    }
}

I put in the Github for future reference.

Apart from the obligation to treat them they are identical to the unverified exception. In practice it decreases the robustness of the applications. One can argue that just because the programmer is sloppy, but in practice...

1

Checked exceptions are exceptions that are identified by language and are amenable to treatment, in turn are the opposite of Unchecked exceptions, but what are Checked? They are exceptions that are treated with a simple Try catch. A input wrong, for example, which can be solved easily. An Unchecked Exception would be an Runtime as a Nullpointerexception, where you can not treat the problem, just inform the user that it occurred and to remedy the problem the only solution is to open again the software or portion of the code accessed. Many professionals do not make use of this good practice of treating exceptions, but it is a useful tool when well used.

Some languages make use of Checked exceptions: Java, Package . NET(C++,C#....)

SOURCE

Browser other questions tagged

You are not signed in. Login or sign up in order to post.