The document contains code examples demonstrating the use of exceptions in C# applications. Example 1.8 catches and displays information about exceptions during integer division. Example 1.9 catches exceptions while counting lines in a file and displays the line count. Example 1.10 demonstrates throwing a custom ArgumentException. Example 1.11 rethrows a caught exception to display its stack trace. Finally, Example 1.12 creates a custom SoaException class and throws an instance of it to test exception handling.
The document contains code examples demonstrating the use of exceptions in C# applications. Example 1.8 catches and displays information about exceptions during integer division. Example 1.9 catches exceptions while counting lines in a file and displays the line count. Example 1.10 demonstrates throwing a custom ArgumentException. Example 1.11 rethrows a caught exception to display its stack trace. Finally, Example 1.12 creates a custom SoaException class and throws an instance of it to test exception handling.
The document contains code examples demonstrating the use of exceptions in C# applications. Example 1.8 catches and displays information about exceptions during integer division. Example 1.9 catches exceptions while counting lines in a file and displays the line count. Example 1.10 demonstrates throwing a custom ArgumentException. Example 1.11 rethrows a caught exception to display its stack trace. Finally, Example 1.12 creates a custom SoaException class and throws an instance of it to test exception handling.
{ class Program { static void Main(string[] args) { try { int a, b; a = int.Parse(Console.ReadLine()); b = int.Parse(Console.ReadLine()); int res = a / b; } catch (Exception exceptii) { Console.WriteLine(exceptii.Message); Console.WriteLine(exceptii.Source); Console.WriteLine(exceptii.StackTrace); Console.WriteLine(exceptii.TargetSite); } Console.ReadKey(); } }
Aplicatia 1.9 using System; using System.IO;
namespace Aplicatia 1.9
{
class Program {
static void Main(string[] args)
{ StreamReader sursa = null; int contor = 0; try { sursa = new StreamReader("D:\\fisier_test.txt"); while (sursa.ReadLine() != null) contor++; } catch (Exception exceptii) { Console.WriteLine(exceptii.Message); } finally { Console.WriteLine("Numarul de linii este" + contor); } Console.ReadKey(); } } } Aplicatia 1.10 using System;