Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
CSharpNotesForProfessionals.pdf
Скачиваний:
57
Добавлен:
20.05.2023
Размер:
6.12 Mб
Скачать

System.DivideByZeroException: Cannot divide by b because it is zero ---> System.DivideByZeroException: Attempted to divide by zero.

at UserQuery.g__DoSomething0_0() in C:[...]\LINQPadQuery.cs:line 36

--- End of inner exception stack trace ---

at UserQuery.g__DoSomething0_0() in C:[...]\LINQPadQuery.cs:line 42 at UserQuery.Main() in C:[...]\LINQPadQuery.cs:line 55

If you're trying this example in LinqPad, you'll notice that the line numbers aren't very meaningful (they don't always help you). But passing a helpful error text as suggested above oftentimes significantly reduces the time to track down the location of the error, which is in this example clearly the line

c = a / b;

in function DoSomething().

Try it in .NET Fiddle

Section 76.12: Nesting of Exceptions & try catch blocks

One is able to nest one exception / try catch block inside the other.

This way one can manage small blocks of code which are capable of working without disrupting your whole mechanism.

try

{

//some code here try

{

//some thing which throws an exception. For Eg : divide by 0

}

catch (DivideByZeroException dzEx)

{

//handle here only this exception

//throw from here will be passed on to the parent catch block

}

finally

{

//any thing to do after it is done.

}

//resume from here & proceed as normal;

}

catch(Exception e)

{

//handle here

}

Note: Avoid Swallowing Exceptions when throwing to the parent catch block

GoalKicker.com – C# Notes for Professionals

462