Presentation is loading. Please wait.

Presentation is loading. Please wait.

C# Exceptions 1 CNS 3260 C#.NET Software Development.

Similar presentations


Presentation on theme: "C# Exceptions 1 CNS 3260 C#.NET Software Development."— Presentation transcript:

1 C# Exceptions 1 CNS 3260 C#.NET Software Development

2 C# Exceptions2 Exceptions in C# Must inherit from System.Exception Must inherit from System.Exception Standard error handling in C# Standard error handling in C# Are thrown when: Are thrown when: the code reaches a throw statement the code reaches a throw statement System exceptions occur (such as divide by zero) System exceptions occur (such as divide by zero) No checked exceptions or exception specifications No checked exceptions or exception specifications

3 C# Exceptions3 throw statement Must throw an instance of an exception: Must throw an instance of an exception: throw(new MyException(“Error”)); May be used by itself only in a catch block: May be used by itself only in a catch block: catch catch { throw; throw; }

4 C# Exceptions4 Creating an Exception Class Inherits from System.Exception or a derived class Inherits from System.Exception or a derived class public class Exception1 : System.Exception { public Exception1(string message) : base(message){} } public class SomeException : Exception1 { public SomeException(string message) : base(message){} }

5 C# Exceptions5 ApplicationException Exceptions defined within an application should extend (inherit from) System.ApplicationException Exceptions defined within an application should extend (inherit from) System.ApplicationException

6 C# Exceptions6 Catching an Exception A catch block is associated with a try block A catch block is associated with a try block A try block may have more than one catch block A try block may have more than one catch block catch blocks catch the exception type or any derived exception types passing through catch blocks catch the exception type or any derived exception types passing through catch blocks are searched in the order they appear in the code catch blocks are searched in the order they appear in the code catch blocks for specific types must come before the more general types catch blocks for specific types must come before the more general types An Empty catch clause will catch any type An Empty catch clause will catch any type catch clauses don’t need a variable name catch clauses don’t need a variable name catch(Exception) is ok catch(Exception) is ok

7 C# Exceptions7 catch blocks void function1() { try { // code } catch(Exception1 ex) { } catch(Exception ex) { } // if no rethrow occurs // execution resumes here } void function1() { try { // code } catch(Exception ex) { } catch(Exception1 ex) { } } Wrong Right

8 C# Exceptions8 Exception Flow Control void function1() { try { try { throw(new SomeOtherException(“Error Message”)); } catch(Exception1 ex) { } catch(Exception2 ex) { } } The exception is passed up until a suitable handler is found The exception is passed up until a suitable handler is found

9 C# Exceptions9 Exception Flow Control void function2() { try { Function1(); } catch(Exception3 ex3) { } catch(Exception2 ex4) { } catch(Exception ex) { } } If no suitable handler (catch clause) was found, the exception is passed to the calling method If no suitable handler (catch clause) was found, the exception is passed to the calling method

10 C# Exceptions10 Unhandled Exceptions If no error handler is found the application terminates If no error handler is found the application terminates Control is passed back to Windows Control is passed back to Windows

11 C# Exceptions11 finally block Must be associated with a try block Must be associated with a try block a try block may have only one finally block a try block may have only one finally block finally block always gets executed finally block always gets executed The appropriate catch clause is executed first The appropriate catch clause is executed first (See Exceptions Demo)

12 C# Exceptions12 finally Flow Control void function1() { try { try { throw(new SomeException(“Error Message”)); } catch(Exception1 ex) { } finally { } catch(Exception2 ex) { } finally { }

13 C# Exceptions13 Some Special Rules Unhandled Exception in a destructor Unhandled Exception in a destructor destructor stops executing, exception is discarded, base destructor is called destructor stops executing, exception is discarded, base destructor is called catch (with no parameter) will catch unmanaged exceptions from other languages catch (with no parameter) will catch unmanaged exceptions from other languages

14 C# Exceptions14 Library Exceptions Feel free to use these: Feel free to use these: ArithmeticException ArithmeticException ArrayTypeMismatchException ArrayTypeMismatchException DivideByZeroException DivideByZeroException IndexOutOfRangeException IndexOutOfRangeException InvalidCastException InvalidCastException NullReferenceException NullReferenceException OutOfMemoryException OutOfMemoryException OverflowException OverflowException StackOverflowException StackOverflowException TypeInitializationException TypeInitializationException

15 C# Exceptions15 System.Exception Class Message Message string message associated with the exception string message associated with the exception InnerException InnerException If this exception was generated inside an exception handler, this refers to the original exception If this exception was generated inside an exception handler, this refers to the original exception Source Source Refers to the source class Refers to the source class StackTrace StackTrace String representing the call stack, file and line number String representing the call stack, file and line number

16 C# Exceptions16 Breaking On Exceptions Debug | Exceptions (or Ctrl + Alt + E) Debug | Exceptions (or Ctrl + Alt + E)

17 C# Exceptions17 Design Considerations Class discussion Class discussion


Download ppt "C# Exceptions 1 CNS 3260 C#.NET Software Development."

Similar presentations


Ads by Google