Query about catching exceptions within a function - C #

0

I was wondering if I could catch all the exceptions within a function, for that I would consider the following example.

My question is mainly if I can catch dividebyzeroexception within the definition of the function and not from outside.

 static int division(int a, int b)
    {
        try
        {
            if (a == 5)
            {
                throw new miexcep();
            }
            return a / b;

        }
        catch (miexcep)
        {
            Console.WriteLine("A no puede ser 5!!");
        }



        return a / b;
    }



try
        {
            division(5, 0);

        }
        catch (DivideByZeroException)
        {
            Console.WriteLine("B no puede ser 0");
        }
    
asked by Shiki 10.03.2018 в 00:54
source

3 answers

1

As the official Microsoft documentation says about try / catch ( link )

  

It is possible to use more than one specific catch clause in the same   try-catch statement In this case, the order of the catch clauses is   important, since catch clauses are examined in order. Detect   the more specific exceptions before the less specific ones. The   compiler generates an error if it sorts the detection blocks so that a   back block can never be reached.

You must sort the catch blocks from more specific to less, since if you enter within a catch block, you will ignore the following ones. An example:

static int division(int a, int b)
{
    try
    {
        if (a == 5)
        {

        }
        return a / b;

    }
    catch (DividedByZeroException)
    {
        Console.WriteLine("Excepción mas específica.");
    }
    catch (Exception)
    {
        Console.WriteLine("Excepción más genérica.");
    }
} 


division(5, 0);

Note: You could also add a finally , which is a block that runs regardless of whether you can execute the inside of the try or not (although it may not be so useful in your case) .

    
answered by 13.03.2018 в 16:51
0

I think you mean the following:

static int division(int a, int b)
    {
        try
        {
            if (a == 5)
            {
                throw new miexcep();
            }
            return a / b;

        }
        catch (miexcep)
        {
            Console.WriteLine("A no puede ser 5!!");
        }
        catch (DivideByZeroException)
        {
            Console.WriteLine("B no puede ser 0");
        }


        return a / b;
    } 



division(5, 0);
    
answered by 10.03.2018 в 01:02
0

I think you approach the issue badly. In your case you want to throw your own exception when a = 5. Then you should think your code like this:

static int division(int a, int b)
{
    if(a==5)
        throw new miexcep();

     try {
         return a / b;
     } catch (divideByZeroException e) {
         throw e;
     }
}

That is, if a == 5, you launch your customized exception directly. In case it is different, the code continues and tries a try-catch to return the division. You capture the exception (in this case the divideByZero) and return it directly.

and to call the division function:

try {
    division(5,0);
} catch (miexcep e) {
    Console.WriteLine("A no puede ser 5");
} catch (DivideByZeroException e) {
    Console.WriteLine("B no puede ser 0");
}
    
answered by 13.03.2018 в 17:04