How to create mandatory exceptions in C#?

Asked

Viewed 129 times

10

Is there any way to make a method make an exception that must be dealt with when the method is invoked?

I used the throw new Exception("mensagem de erro") but the treatment is still optional when invoking the method.

3 answers

6


Luckily it doesn’t. This what you want is called checked Exception and has a lot of problems that is not the case now. Beyond what if there were people would abuse. It’s not enough that people think they should shoot and capture Exception?

The form used is to document and pray. But you can create a static analysis tool that checks this in the code. With the new .NET Compiler Platform make one became much easier and can integrate to Visual Studio. It is not so simple, it depends on the programmer use the tool but help.

Creating tests really helps. Visual Studio can create many tests automatically for you through Smart Unit Tests, including tests that generate exceptions to see if you’ve taken care of everything. It is a solution for those who program in this way.

An important detail is that situations where this is really necessary are relatively rare. In general the methods must be able to function independent of the capture of the exception or must break even.

Maybe the code generators will help.

  • Really the question came from my experience with JAVA and as I was now studying exceptions in C# and I realized this difference of functioning I preferred to end the doubt before moving forward in the language. Thanks for the feedback.

  • You’re doing it right. In Java there’s a huge abuse of the exceptions and even more of the checks. It’s a culture of language. In C#, at least among good programmers, its use is more controlled. One opts for them when the situation is really exceptional. I talk about it a lot here: http://answall.com/search?q=user:101+[exce%C3%A7%C3%A3o]

2

1

This is something proper to the Java copier, which requires certain exceptions to be dealt with. This is stated when signing the method with the declaration throws, as shown below:

public void drinkCoffee(CoffeeCup cup) throws
        TooColdException, TooHotException {
        int temperature = cup.getTemperature();
        if (temperature <= tooCold) {
            throw new TooColdException();
        }
        else if (temperature >= tooHot) {
            throw new TooHotException();
        }
        //...
    }

Well, c# doesn’t require you to handle exceptions. But when you program, it’s easy to imagine things that can generate exceptions. And they should always be dealt with, especially if you’re developing a more commercial solution, which will go into production, or something aimed at users in general.

Remember that handling exceptions and minimizing them to the maximum is good programming practice, even if the language leaves this as something optional. When you do this, you make your code more secure and will avoid various problems with customer complaints in the future.

  • 1

    Quite the contrary, exceptions should be treated only when they really need, otherwise, do not treat, This is one of the biggest myths of programming. Treating exception unnecessarily is bad practice. The language leaves it optional because it should be optional. Unfortunately there is no way to prevent the improper capture of the exception. Who does this often makes the code less reliable because it only hides the problem when it can’t do anything useful and specialized.

  • Treating means minimizing the impact that exceptions can have on your application, not hiding it. Sometimes with simple validations you can prevent several exceptions from occurring. That’s why I said to treat and minimize.

  • 1

    This is not treating exception like you said, this is preventing it from happening. Yes this is right, but your response encourages treatment all the time. Only the minimizing part is that it is right and almost hidden and ambiguous in the way it is written. Treating and minimizing are antagonistic things.

Browser other questions tagged

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