You need to be a premium member to use this feature. To access it, you'll have to upgrade your membership.
Become a sharper developer and jumpstart your career.
$0
$
. 00
monthly
For Basic members:
$10
For Premium members:
In C# Exception is a class in the system namespace. An object of an exception is that describe the exceptional conditions occur in a code That means, we are catching an exception, creating an object of it, and then throwing it. C# supports exceptions in a very much the same way as Java and C++.
Definition:"EXCEPTION IS A RUNTIME ERROR WHICH ARISES BECAUSE OF ABNORMAL CONDITION IN A CODE SEQUENCE. "In C# Exception is a class in the system namespace. An object of an exception is that describe the exceptional conditions occur in a code That means, we are catching an exception, creating an object of it, and then throwing it. C# supports exceptions in a very much the same way as Java and C++.Before going into detail, I must say the usefulness of knowing and performing exception handling :
Exceptions are handled by using try...catch statements. Code which may give rise to exceptions is enclosed in a try block, which is followed by one or more catch blocks. Well if you don't use try...catch, you could get errors like the following:
class A {static void Main() {catch {}}} TEMP.cs(3,5): error CS1003: Syntax error, 'try' expected
class A {static void Main() {finally {}}} TEMP.cs(3,5): error CS1003: Syntax error, 'try' expected
class A {static void Main() {try {}}} TEMP.cs(6,3): error CS1524: Expected catch or finallyThe try block contains the code segment expected to raise an exception. This block is executed until an exception is thrown The catch block contains the exception handler. This block catches the exception and executes the code written in the block. If we do not know what kind of exception is going to be thrown we can simply omit the type of exception. We can collect it in Exception object as shown in the following program:int a, b = 0 ;Console.WriteLine( "My program starts " ) ;try{ a = 10 / b;}catch ( Exception e ){Console.WriteLine ( e ) ;}Console.WriteLine ( "Remaining program" ) ;The output of the program is:
My program startsSystem.DivideByZeroException: Attempted to divide by zero. at ConsoleApplication4.Class1.Main(String[] args) in d:\dont delete\c#(c sharp)\swapna\programs\consoleapplication4\ consoleapplication4\class1.cs:line 51Remaining program
The exception 'Divide by zero' was caught, but the execution of the program did not stop. There are a number of exception classes provided by C#, all of which inherit from the System.Exception class. Following are some common exception classes:
The finally block is used to do all the clean up code. It does not support the error message, but all the code contained in the finally block is executed after the exception is raised. We can use this block along with try...catch and only with catch too. The finally block is executed even if the error is raised. Control is always passed to the finally block regardless of how the try blocks exits.int a, b = 0 ;Console.WriteLine( "My program starts" ) ;try{a = 10 / b;}catch ( InvalidOperationException e ){Console.WriteLine ( e ) ;}catch ( DivideByZeroException e){Console.WriteLine ( e ) ;}finally{Console.WriteLine ( "finally" ) ;}Console.WriteLine ( "Remaining program" ) ;The output here is:My program startsSystem.DivideByZeroException: Attempted to divide by zero.at ConsoleApplication4.Class1.Main(String[] args) in d:\dont delete\c# (c sharp)\swapna\programs\consoleapplication4\ consoleapplication4\class1.cs:line 51finallyRemaining programBut then what's the difference? We could have writtenConsole.WriteLine ("finally");after the catch block, and not write the finally block at all. Writing finally did not make much of a difference. Anyway the code written after catch gets executed.The answer to this is not clear in this program. It will be clear when we see the try-finally and the throw statement.int a, b = 0 ;Console.WriteLine( "My program starts" ) try{a = 10 / b;}finally{Console.WriteLine ( "finally" ) ;}Console.WriteLine ( "Remaining program" ) ;Here the output isMy program startsException occurred: System.DivideByZeroException: Attempted to divide by zero.at ConsoleApplication4.Class1. Main(String[] args) in d:\dont delete\c# (c sharp) \swapna\programs\consoleapplication4\consoleapplication4 \class1.cs:line 51finallyNote that "Remaining program" is not printed out. Only "finally" is printed which is written in the finally block. The throw statement throws an exception. A throw statement with an expression throws the exception produced by evaluating the expression. A throw statement with no expression is used in the catch block. It re-throws the exception that is currently being handled by the catch block.int a, b = 0 ;Console.WriteLine( "My program starts" ) ;try{a = 10 / b;}catch ( Exception e){throw}finally{Console.WriteLine ( "finally" ) ;}This shows that the exception is re-thrown. Whatever is written in finally is executed and the program terminates. Note again that "Remaining program" is not printed.
Pro WPF: Windows Presentation Foundation in .NET 3.0