Notes - Exception Handling Notes | EduRev

: Notes - Exception Handling Notes | EduRev

 Page 1


1
Chapter 12 – Exception Handling
Outline
12.1  Introduction
12.2  Exception Handling Overview
12.3 Example: Divide by Zero Without Exception Handling
12.4 Example:  Handling DivideByZeroExceptions
and FormatExceptions
12.5  .NET Exception Hierarchy
12.6  finally Block
© 2002 Prentice Hall.  All rights reserved.
12.6  finally Block
12.7  Exception Properties
12.8  User-Defined  (Programmer-Defined) Exception Classes
[SKIP: 11.8  Handling Overflows with Operators checked and unchecked
Many slides modified by Prof. L. Lilien (even many 
without an explicit message).
Slides added by L.Lilien are © 2006 Leszek T. Lilien.
Permision to use for non-commercial purposes slides added by L.Lilien’s will be gladly 
granted upon a written (e.g., emailed) request.
Page 2


1
Chapter 12 – Exception Handling
Outline
12.1  Introduction
12.2  Exception Handling Overview
12.3 Example: Divide by Zero Without Exception Handling
12.4 Example:  Handling DivideByZeroExceptions
and FormatExceptions
12.5  .NET Exception Hierarchy
12.6  finally Block
© 2002 Prentice Hall.  All rights reserved.
12.6  finally Block
12.7  Exception Properties
12.8  User-Defined  (Programmer-Defined) Exception Classes
[SKIP: 11.8  Handling Overflows with Operators checked and unchecked
Many slides modified by Prof. L. Lilien (even many 
without an explicit message).
Slides added by L.Lilien are © 2006 Leszek T. Lilien.
Permision to use for non-commercial purposes slides added by L.Lilien’s will be gladly 
granted upon a written (e.g., emailed) request.
2
12.1 Introduction
• Exception
– Indication of a problem during program execution
• Problems are exceptional, normally no problems
• Exception handling
– Enables programmers to create application that can handle 
exceptions
© 2002 Prentice Hall.  All rights reserved.
exceptions
• Enable clear, robust and more fault-tolerant programs
• Two kinds of exception handling:
– In many cases, exception handling allows to continue
‘correct’ program execution
– In other more serious cases, exception handling notifies user 
of a problem and terminates program
Slide modified by L. Lilien
Page 3


1
Chapter 12 – Exception Handling
Outline
12.1  Introduction
12.2  Exception Handling Overview
12.3 Example: Divide by Zero Without Exception Handling
12.4 Example:  Handling DivideByZeroExceptions
and FormatExceptions
12.5  .NET Exception Hierarchy
12.6  finally Block
© 2002 Prentice Hall.  All rights reserved.
12.6  finally Block
12.7  Exception Properties
12.8  User-Defined  (Programmer-Defined) Exception Classes
[SKIP: 11.8  Handling Overflows with Operators checked and unchecked
Many slides modified by Prof. L. Lilien (even many 
without an explicit message).
Slides added by L.Lilien are © 2006 Leszek T. Lilien.
Permision to use for non-commercial purposes slides added by L.Lilien’s will be gladly 
granted upon a written (e.g., emailed) request.
2
12.1 Introduction
• Exception
– Indication of a problem during program execution
• Problems are exceptional, normally no problems
• Exception handling
– Enables programmers to create application that can handle 
exceptions
© 2002 Prentice Hall.  All rights reserved.
exceptions
• Enable clear, robust and more fault-tolerant programs
• Two kinds of exception handling:
– In many cases, exception handling allows to continue
‘correct’ program execution
– In other more serious cases, exception handling notifies user 
of a problem and terminates program
Slide modified by L. Lilien
3
12.2 Exception Handling Overview
• Pseudocode with included error processing code
Perform a task
If the preceding task did not execute correctly
Perform error processing
Perform next task
© 2002 Prentice Hall.  All rights reserved.
If the preceding task did not execute correctly
Perform error processing
…
Slide added by L. Lilien
Page 4


1
Chapter 12 – Exception Handling
Outline
12.1  Introduction
12.2  Exception Handling Overview
12.3 Example: Divide by Zero Without Exception Handling
12.4 Example:  Handling DivideByZeroExceptions
and FormatExceptions
12.5  .NET Exception Hierarchy
12.6  finally Block
© 2002 Prentice Hall.  All rights reserved.
12.6  finally Block
12.7  Exception Properties
12.8  User-Defined  (Programmer-Defined) Exception Classes
[SKIP: 11.8  Handling Overflows with Operators checked and unchecked
Many slides modified by Prof. L. Lilien (even many 
without an explicit message).
Slides added by L.Lilien are © 2006 Leszek T. Lilien.
Permision to use for non-commercial purposes slides added by L.Lilien’s will be gladly 
granted upon a written (e.g., emailed) request.
2
12.1 Introduction
• Exception
– Indication of a problem during program execution
• Problems are exceptional, normally no problems
• Exception handling
– Enables programmers to create application that can handle 
exceptions
© 2002 Prentice Hall.  All rights reserved.
exceptions
• Enable clear, robust and more fault-tolerant programs
• Two kinds of exception handling:
– In many cases, exception handling allows to continue
‘correct’ program execution
– In other more serious cases, exception handling notifies user 
of a problem and terminates program
Slide modified by L. Lilien
3
12.2 Exception Handling Overview
• Pseudocode with included error processing code
Perform a task
If the preceding task did not execute correctly
Perform error processing
Perform next task
© 2002 Prentice Hall.  All rights reserved.
If the preceding task did not execute correctly
Perform error processing
…
Slide added by L. Lilien
4
12.2 Exception Handling Overview
• Including error processing code in a program intermixes 
program logic with error-handling logic
– Difficult to read, maintain, debug
– Better to separate program logic from error-handling logic
• Errors happen infrequently 
• If many errors, placing checks for errors in program slows it down 
unnecessarily
© 2002 Prentice Hall.  All rights reserved.
unnecessarily
– Unnecessarily bec. most checks will not find errors
• Exception handling allows to remove error-handling code from
the “main line” program execution
– Improves program clarity
– Enhances modifiability
Slide modified by L. Lilien
Page 5


1
Chapter 12 – Exception Handling
Outline
12.1  Introduction
12.2  Exception Handling Overview
12.3 Example: Divide by Zero Without Exception Handling
12.4 Example:  Handling DivideByZeroExceptions
and FormatExceptions
12.5  .NET Exception Hierarchy
12.6  finally Block
© 2002 Prentice Hall.  All rights reserved.
12.6  finally Block
12.7  Exception Properties
12.8  User-Defined  (Programmer-Defined) Exception Classes
[SKIP: 11.8  Handling Overflows with Operators checked and unchecked
Many slides modified by Prof. L. Lilien (even many 
without an explicit message).
Slides added by L.Lilien are © 2006 Leszek T. Lilien.
Permision to use for non-commercial purposes slides added by L.Lilien’s will be gladly 
granted upon a written (e.g., emailed) request.
2
12.1 Introduction
• Exception
– Indication of a problem during program execution
• Problems are exceptional, normally no problems
• Exception handling
– Enables programmers to create application that can handle 
exceptions
© 2002 Prentice Hall.  All rights reserved.
exceptions
• Enable clear, robust and more fault-tolerant programs
• Two kinds of exception handling:
– In many cases, exception handling allows to continue
‘correct’ program execution
– In other more serious cases, exception handling notifies user 
of a problem and terminates program
Slide modified by L. Lilien
3
12.2 Exception Handling Overview
• Pseudocode with included error processing code
Perform a task
If the preceding task did not execute correctly
Perform error processing
Perform next task
© 2002 Prentice Hall.  All rights reserved.
If the preceding task did not execute correctly
Perform error processing
…
Slide added by L. Lilien
4
12.2 Exception Handling Overview
• Including error processing code in a program intermixes 
program logic with error-handling logic
– Difficult to read, maintain, debug
– Better to separate program logic from error-handling logic
• Errors happen infrequently 
• If many errors, placing checks for errors in program slows it down 
unnecessarily
© 2002 Prentice Hall.  All rights reserved.
unnecessarily
– Unnecessarily bec. most checks will not find errors
• Exception handling allows to remove error-handling code from
the “main line” program execution
– Improves program clarity
– Enhances modifiability
Slide modified by L. Lilien
5
12.2 Exception Handling Overview
• Keywords for exception handling
– try
{
<block of code in which exceptions might occur> 
}
– catch (<exception_parameter>) // 1 or more following each try
{
© 2002 Prentice Hall.  All rights reserved.
{
<how to handle this type of exception>
}
Note:  If no “(<exception_parameter>)”, catch handles all exception types
– finally // optional, follows catch
{
<codes present here will always execute – whether exception or not> 
}
Slide added by L. Lilien
Read More
Offer running on EduRev: Apply code STAYHOME200 to get INR 200 off on our premium plan EduRev Infinity!