0% found this document useful (0 votes)
72 views

Chapter 3 - Exception Handling

The document discusses exception handling in C# programming. It defines what exceptions are and provides examples. It describes the keywords used for exception handling in C# - try, catch, finally, throw, throws. It explains how to handle exceptions using these keywords and discusses checked and unchecked exceptions in C#. The document is a chapter from a book on event driven programming in C#.

Uploaded by

dagne
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
72 views

Chapter 3 - Exception Handling

The document discusses exception handling in C# programming. It defines what exceptions are and provides examples. It describes the keywords used for exception handling in C# - try, catch, finally, throw, throws. It explains how to handle exceptions using these keywords and discusses checked and unchecked exceptions in C#. The document is a chapter from a book on event driven programming in C#.

Uploaded by

dagne
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 13

Event driving Programming

Chapter 3:
Exception Handling

Event driving Programming by(C#) byDagne w . 1


Exception handling keyword
Try
Finally
Catch
Throw

C# by Dagne w 2
Exception Handling
Exception Handling
 An exception is unforeseen error that occur when a
program is running.
 Examples:
trying to read from a file that does not exist, throws
FileNotFoundException.
Trying to read from a database table that does not exist,
throws a SqlException.
Showing actual unhandled exceptions to the end user is
bad for two reasons 1. Users will be annoyed as they are
cryptic and does not make much sense to the end users. 2.
Exceptions contain information, that can be used for
hacking
C# by Dagne w 3
Exception Handling Keywords
The exception handling in c# is one of the
powerful mechanism to handle the runtime
errors such as ClassNotFound, IO, SQL, Remote etc.
There are 5 keywords used in C# exception handling.
• try
• catch
• finally
• throw
• throws

OOP using Java compiled by Dagne W 4


Exception Handling
An exception is actually a class that derives from
System.Exception class. The System.Exception class has
several useful properties, that provide valuable information
about exception.
Message: Gets a message that describes the current
exception
Stack Trace: Provides the call stack to the line number in
he method where the exception occurred.
Source: gets or sets the name of the application or object
that causes the error.
ToString(): creates and return a string representation of the
current exception

C# by Dagne w 5
Exception handling

C# by Dagne w 6
c# Exception Handling …
using System;  
public class ExExample  
{  
    public static void Main(string[] args)  
    {  
        try  
        {  
            int a = 10;  
            int b = 0;  
            int x = a / b;  
        }  
        catch (Exception e) { Console.WriteLine(e); }  
        finally { Console.WriteLine("Finally block is executed"); }  
        Console.WriteLine("Rest of the code");  
    }  
}  

C# by Dagne w 7
Exception
C# Checked and Unchecked
C# provides checked and unchecked keyword to
handle integral type exceptions. Checked and
unchecked keywords specify checked context and
unchecked context respectively. In checked context,
arithmetic overflow raises an exception whereas, in an
unchecked context, arithmetic overflow is ignored and
result is truncated.
C# Checked
The checked keyword is used to explicitly check
overflow and conversion of integral type values at
compile time.
.
C# by Dagne w 8
C# System Exception class

The SystemException is a predefined exception


class in C#. It is used to handle system related
exceptions. It works as base class for system
exception namespace. It has various child classes
like: ValidationException, ArgumentException,
ArithmeticException, DataException,
StackOverflowException etc.

C# by Dagne w 9
Checked Exceptions
The classes that extend Throwable class except
RuntimeException and Error are known as
checked exceptions
 Example.IOException, SQLException etc.
Checked exceptions are checked at compile-time.

C# by Dagne w 10
Unchecked Exception

The classes that extend RuntimeException are


known as unchecked exceptions
 Example. ArithmeticException,
NullPointerException,
ArrayIndexOutOfBoundsException etc.
Unchecked exceptions are not checked at compile-
time rather they are checked at runtime.

C# by Dagne w 11
Error Exceptions

 Error is irrecoverable e.g. OutOfMemoryError,


VirtualMachineError, AssertionError etc.

C# by Dagne w 12
THANK YOU!!

C# by Dagne w 13

You might also like