Object Oriented Programming using C#

(backadmin) #1
Object Oriented Programming using C#
Creating And Using Eceptions

Thus we could

a) write a catch block that would react to ALL of these exceptions by catching a SystemException, or we could
b) write a catch block that would catch any type of input \ output exceptions, IOExceptions, and ignore all
others or
c) we could be even more specific and catch only FileNotFoundExceptions.

Catching specific exceptions allows us to take specific remedial action e.g. given a FileNotFoundException we could explain
to the user of a program that the file was missing and allow them to specify an alternative file.

Catching general exceptions allows us to ensure no potentially fatal error causes our program to crash though we may
not be able to take very useful remedial action e.g. given any Exception we could close our program down but without
knowing what caused the exception we could not take specific remedial action.

In exactly the same way we can define sub classes of ApplicationException and catch these exception as and when
appropriate.

9.4 Throwing Exceptions
Having defined our own exception classes we must then ensure our methods generate, or throw, these exceptions when
appropriate. For example we would instruct a GetClient() method to throw an UnknownClientException when a client
cannot be found with the specified ID.

© UBS 2010. All rights reserved.

http://www.ubs.com/graduates

Looking for a career where your ideas could really make a difference? UBS’s
Graduate Programme and internships are a chance for you to experience
for yourself what it’s like to be part of a global team that rewards your input
and believes in succeeding together.

Wherever you are in your academic career, make your future a part of ours
by visiting http://www.ubs.com/graduates.

You’re full of energy


and ideas. And that’s


just what we are looking for.


Please click the advert

Free download pdf