CIT 383 Administrative Scripting Exceptions Computer Security Art
- Slides: 18
CIT 383: Administrative Scripting Exceptions Computer Security: Art and Science 1
Topics 1. 2. 3. 4. 5. 6. Subclasses and Inheritance What is an Exception? Raising Exceptions Catching Exceptions Retry Exception Classes CIT 383: Administrative Scripting
Subclassing A subclass is a class that is based on but modified from an existing class. § A subclass extends another class. § Extends means the same as inherits from. § The original class is known as the superclass. A subclass is defined using < superclass: class Point 3 D < Point end CIT 383: Administrative Scripting
Inheritance Subclasses inherit methods of superclass. Superclass methods can be called on subclass. p 3 d = Point 3 D. new(0, 0, 0) p 3 d. x p 3 d. y Class variables shared with superclass. If subclass modifies value of a class variable, the superclass sees the modified value too. CIT 383: Administrative Scripting
Overiding and Adding Methods To make Point 3 D useful, redefine initialize: class Point 3 D < Point def initialize(x, y, z) @x, @y, @z = x, y, z end We also need to add accessors for the z coordinate: def z @z end def z=(value) @z = value end CIT 383: Administrative Scripting
Calling Superclass Methods How can you call an overidden method? Use super(args) in method of subclass. New definition of initialize: class Point 3 D < Point def initialize(x, y, z) # pass first two args to # superclass initialize method super(x, y) # initialize third argument @z = z end CIT 383: Administrative Scripting
What is an Exception? An exception is an object that represents an exceptional condition, such as an error of some type. An exception is raised when an error occurs. A ruby program terminates on an exception. An exception can be caught by an exception handler, preventing program termination. CIT 383: Administrative Scripting
Exception Classes Exceptions belong to Exception class or its subclasses. Exception No. Memory. Error System. Exit Standard. Error Argument. Error IOError Index. Error Type. Error Zero. Division. Error CIT 383: Administrative Scripting
Raising Exceptions The raise method raises an exception raise: creates a new Runtime. Error and raises it. raise(string): creates new Runtime. Error with the specified string as its message and raises it. raise(class): creates a new exception object of the specified class and raises it. raise(class, string): creates a new exception object of the specified class with the specified string as its message and raises it. CIT 383: Administrative Scripting
Raise Example Raise an exception on a bad username argument: class User def initialize(username) if username =~ /^[a-z][a-z 0 -9]+$/ @username = username else raise 'Invalid username' end end CIT 383: Administrative Scripting
Handling Exceptions Use a rescue clause with a begin statement. begin # Ruby code that may raise # an exception. rescue # Exception-handling code. end CIT 383: Administrative Scripting
Handling Exceptions by Type Specify the exception class after rescue By default, only Standard. Error is caught. To catch any exception rescue Exception To catch specific exception and assign to e. rescue Argument. Error => e Accessing the exception object string puts e. message CIT 383: Administrative Scripting
Catching Multiple Exception Types Handling multiple types identically: rescue Argument. Error, IOError => e Handling each type differently: begin #code that may raise exception rescue Argument. Error => e # code to handle Argument. Errors rescue IOError => e # code to handle IOErrors end CIT 383: Administrative Scripting
Retry restarts at top of begin block. # Try to get URL up to 10 times in case svr slow tries = 0 begin tries += 1 geturl(‘http: //example. com/’) rescue Exception => e puts e. message # print error if tries < 10 sleep(10) # wait 10 seconds retry # try get_url again end CIT 383: Administrative Scripting
Writing Exception Handlers Keep begin blocks short. begin # no more than 3 lines rescue end Move longer blocks into a new method. begin try_to_geturl() rescue end CIT 383: Administrative Scripting
Defining Exceptions What is the purpose of exception classes? Each class can be handled differently by its own rescue block. Creating a new exception class My. Error < Standard. Error end Exception subclasses define no new methods Only characteristics that matter are their name and parent class. CIT 383: Administrative Scripting
Defining Exceptions Create a new exception class only when The caller has to do something different to recover from the error. Do not create a class for each error message Use the message string to contain the message instead of creating a new exception class. CIT 383: Administrative Scripting
References 1. Michael Fitzgerald, Learning Ruby, O’Reilly, 2008. 2. David Flanagan and Yukihiro Matsumoto, The Ruby Programming Language, O’Reilly, 2008. 3. Hal Fulton, The Ruby Way, 2 nd edition, Addison. Wesley, 2007. 4. Robert C. Martin, Clean Code, Prentice Hall, 2008. 5. Dave Thomas with Chad Fowler and Andy Hunt, Programming Ruby, 2 nd edition, Pragmatic Programmers, 2005. Computer Security: Art and Science 18
- Private securit
- Cmpt 383
- Ieee 383
- Finance 383
- Mcit 592
- Cit 595
- Cit593
- Enhanced security administrative environment
- Spl exceptions
- Robust programs
- Stark law exceptions
- Present continuous questions
- Exception handler php
- Coccus
- Koch's postulates exceptions
- Pasteurization
- Stark law exceptions
- Predefined and non predefined exceptions are raised
- What is first ionization