Mahmoud Rafeek Alfarra Computer Programming || Chapter 2: Exception handling.

Slides:



Advertisements
Similar presentations
Exceptions Chapter Throwing and Catching Exceptions When a program runs into a problem that it cannot handle, it throws an exception. Exceptions.
Advertisements

Exceptions Don’t Frustrate Your User – Handle Errors KR – CS 1401 Spring 2005 Picture – sysprog.net.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 15: Exception Handling.
Try…Catch…Finally Blocks ( continued ) Generic catch clause –Omit argument list with the catch –Any exception thrown is handled by executing code within.
C# Programming: From Problem Analysis to Program Design1 Debugging and Handling Exceptions C# Programming: From Problem Analysis to Program Design 3 rd.
Chapter 16: Exception Handling C++ Programming: From Problem Analysis to Program Design, Fifth Edition.
Objectives In this chapter you will: Learn what an exception is Learn how to handle exceptions within a program See how a try / catch block is used to.
Exception Handling Yaodong Bi Exception Handling Java exception handling Try blocks Throwing and re-throwing an exception Catching an.
Chapter 11 Debugging and Handling Exceptions
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 16: Exception Handling.
Slides prepared by Rose Williams, Binghamton University ICS201 Exception Handling University of Hail College of Computer Science and Engineering Department.
Exceptions Briana B. Morrison CSE 1302C Spring 2010.
Handling Errors during the Program Execution Svetlin Nakov Telerik Corporation
Understand Error Handling Software Development Fundamentals LESSON 1.4.
1 Lecture 11 Interfaces and Exception Handling from Chapters 9 and 10.
Microsoft VB 2005: Reloaded, Advanced Chapter 5 Input Validation, Error Handling, and Exception Handling.
EXCEPTIONS Def: An exception is a run-time error. Examples include: attempting to divide by zero, or manipulate invalid data.
Exception Handling An Exception is an indication of a problem that occurs during a program’s execution. Exception handling enables the programmer to create.
Exceptions. 2 Objectives Introduce C# exception handling –library exception types –custom exceptions Describe keywords used for exception handling –try.
 2002 Prentice Hall. All rights reserved Exception-Handling Overview Exception handling –improves program clarity and modifiability by removing.
Chapter 12: Advanced Topics: Exception Handling Visual Basic.NET Programming: From Problem Analysis to Program Design.
Chapter 11: Handling Exceptions and Events J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Fourth.
Exception Handling. Exceptions and Errors When a problem encounters and unexpected termination or fault, it is called an exception When we try and divide.
Chapter 13 Exception Handling F Claiming Exceptions F Throwing Exceptions F Catching Exceptions F Rethrowing Exceptions  The finally Clause F Cautions.
Dr. Abraham. Exception Any problem that VB or OS could not handle Robust program A program that performs well not only under ordinary conditions but also.
 2002 Prentice Hall. All rights reserved. 1 Exception Handling Overview Exception handling –Enable clear, robust and more fault-tolerant programs –Process.
1 Chapter Eight Exception Handling. 2 Objectives Learn about exceptions and the Exception class How to purposely generate a SystemException Learn about.
Exception Handling in Java Exception Handling Introduction: After completing this chapter, you will be able to comprehend the nature and kinds.
UNIT 3 TEMPLATE AND EXCEPTION HANDLING. Introduction  Program errors are also referred to as program bugs.  A C program may have one or more of four.
COMPUTER PROGRAMMING 2 Exceptions. What are Exceptions? Unexpected events that happen when the code is executing (during runtime). Exceptions are types.
Pemrograman VisualMinggu …12… Page 1 MINGGU Ke Duabelas Pemrograman Visual Pokok Bahasan: Exception Handling Tujuan Instruksional Khusus: Mahasiswa dapat.
Exception. Runtime Error Consider the following program: public class BadArray { public static void main(String[] args) { // Create an array with three.
VB.Net - Exceptions Copyright © Martin Schray
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 10.
10/20/2015Assoc. Prof. Stoyan Bonev1 COS240 O-O Languages AUBG, COS dept Lecture 37 Title: C# vs. Java Exception Handling Reference: COS240 Syllabus.
Chapter 14: Exception Handling. Objectives In this chapter, you will: – Learn what an exception is – Learn how to handle exceptions within a program –
Exception Handling Unit-6. Introduction An exception is a problem that arises during the execution of a program. An exception can occur for many different.
Exceptions in Java. Exceptions An exception is an object describing an unusual or erroneous situation Exceptions are thrown by a program, and may be caught.
Sheet 3 HANDLING EXCEPTIONS Advanced Programming using Java By Nora Alaqeel.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 11 Handling Exceptions and Events.
1 cuberoot /* Solve f(x) = x*x*x-5 = 0 f'(x) = 3x^2 */ public class cuberoot{ public static void main(String args[]){ double error= ; double x0.
Exceptions in C++. Exceptions  Exceptions provide a way to handle the errors generated by our programs by transferring control to functions called handlers.
Exception Handling in Java Topics: Introduction Errors and Error handling Exceptions Types of Exceptions Coding Exceptions Summary.
Mahmoud Rafeek Alfarra Computer Programming || Chapter 1: Introduction & OOP.
Chapter 15: Exception Handling C++ Programming: Program Design Including Data Structures, Fifth Edition.
CS212: Object Oriented Analysis and Design Lecture 19: Exception Handling.
Exception-Handling Fundamentals  A Java exception is an object that describes an exceptional (that is, error) condition that has occurred in a piece of.
ZHANG Yu, Intelligence Engineering Lab at College of Computer Science and Technology in Jllin University 1 Programming in C#.NET.
Text INTRODUCTION TO ASP.NET. InterComm Campaign Guidelines CONFIDENTIAL Simply Server side language Simplified page development model Modular, well-factored,
Exception Handling SWE 344 Internet Protocols & Client Server Programming.
Exception Handling How to handle the runtime errors.
C# Exceptions 1 CNS 3260 C#.NET Software Development.
Exception and Exception Handling. Exception An abnormal event that is likely to happen during program is execution Computer could run out of memory Calling.
ECE122 L23: Exceptions December 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 24 Exceptions.
1 Handling Errors and Exceptions Chapter 6. 2 Objectives You will be able to: 1. Use the try, catch, and finally statements to handle exceptions. 2. Raise.
Garbage Collection It Is A Way To Destroy The Unused Objects. To do so, we were using free() function in C language and delete() in C++. But, in java it.
Agenda Introduction Errors and Exception Exception Hierarchy Classification of Exceptions Built in Exceptions Exception Handling in Java User defined.
Lecture 11 Dr. Eng. Ibrahim El-Nahry Exception Handling.
Asif Nawaz University Institute of Information Technology, PMAS-AAUR Lecture 05: Object Oriented Programming:2014 Object-Oriented Programming in C++ Exception.
16 Exception Handling.
Debugging and Handling Exceptions
Why exception handling in C++?
Debugging and Handling Exceptions
CNS 3260 C# .NET Software Development
Exception Handling Chapter 9.
Chapter 12 Exception Handling and Text IO
Exceptions Problems in a Java program may cause exceptions or errors representing unusual or invalid processing. An exception is an object that defines.
Exception Handling Chapter 9 Edited by JJ.
Exception Handling By: Enas Naffar.
Chapter 13 Exception Handling: A Deeper Look
Presentation transcript:

Mahmoud Rafeek Alfarra Computer Programming || Chapter 2: Exception handling

Syllabus  Revision of OOP  Exception Handling  String manipulation  Regular expression  Files and Streams  Connect applications with DBMS  Streams-Based Sockets and Datagrams 2

Contents Example Exception Classes in C# Syntax Exception handling What is an exception error? Practices User defined Exception

What is an exception error?  An exception is a problem that arises during the execution of a program.  A C# exception is a response to an exceptional situation that arises while a program is running, such as an attempt to divide by zero

What is an exception error?  Exceptions provide a way to transfer control from one part of a program to another

Exception handling  C# exception handling is built upon four keywords:  try: A try block identifies a block of code for which particular exceptions will be activated. It's followed by one or more catch blocks.  catch: A program catches an exception with an exception handler at the place in a program where you want to handle the problem. The catch keyword indicates the catching of an exception

Exception handling  C# exception handling is built upon four keywords:  finally: The finally block is used to execute a given set of statements, whether an exception is thrown or not thrown.  For example, if you open a file, it must be closed whether an exception is raised or not.  throw: A program throws an exception when a problem shows up. This is done using a throw keyword

Syntax  Assuming a block will raise and exception, a method catches an exception using a combination of the try and catch keywords.  You can list down multiple catch statements to catch different type of exceptions in case your try block raises more than one exception in different situations

Syntax 9

Syntax  C# exceptions are represented by classes.  The exception classes in C# are mainly directly or indirectly derived from the System.Exception class.  Some of the exception classes derived from the System.  Exception class are the System.ApplicationException and System.SystemException classes

Syntax  The System.ApplicationException class supports exceptions generated by application programs.  So the exceptions defined by the programmers should derive from this class.  The System.SystemException class is the base class for all predefined system exception

Exception Classes in C# There are another Exception classes in C#, search about them !!

Example 1 1.class Program { 2. public static void division(int num1, int num2) 3. { 4. float result=0.0f; 5. try 6. { 7. result = num1 / num2; 8. } 9. catch (DivideByZeroException e) 10. { 11. Console.WriteLine("Exception Error !! \n divid by zero !!"); 12. // Console.WriteLine("Exception caught: {0}", e); 13. } 14. finally 15. { 16. Console.WriteLine("Result: {0} ", result); 17. } 18. } 19. static void Main(string[] args) 20. { 21. division(10,0); 22. Console.ReadLine(); 23. } }

Example 2 1. catch (DivideByZeroException ex2) 2. { 3. // ….. 4. } 5. catch (FormatException ex1) 6. { 7. //…… 8. } Try ex1.Message Try ex1.Message Try Exception only Instead of DivideByZeroException Or FormatException Try Exception only Instead of DivideByZeroException Or FormatException

Practice Use IndexOutOfRangeException & FormatException & Exception Use IndexOutOfRangeException & FormatException & Exception

User Defined Exception Every user defined exception must be defined as a new class Every user defined exception must be derived from ApplicationException ApplicationException New Exception Class

User Defined Exception using System; namespace ExceptionHandling { class NegativeNumberException : ApplicationException { public NegativeNumberException(string message) // show message } if(value<0) throw new NegativeNumberException(" Use Only Positive numbers");

Mahmoud Rafeek Alfarra