Document Sample
error Powered By Docstoc
					Episode #11
Driss Zouak
   Error Handling
       Exceptions vs. Errors
       Handling Exceptions
       Preventing Validator controls
   Tracing and Debugging
   Lab – Work on your assignments
Mid Term – Q1a
Q: Communication between client/server
                     1 - HTTP Request

  Web Browser
                                         2 – Web Server
                                        Receives Request

                                     3 – ASP.NET process
       4 - HTTP
                             Web         Runs the page
   Response (HTML)
Mid Term – Q1b
Q: Explain MSIL & CLR
   MSIL – Microsoft Intermediate
   Language. Source code is compiled to
   MSIL by the language compiler

CLR – Common Language Runtime. It
  takes the MSIL executable or DLL are
  compiles it to native code (only done
  once, not every time), and runs it.
Mid Term – Q2
Q: Which of the following are features of server
    controls but not of html controls?
a – Makes it easier to develop web user
    interfaces in ASP.NET
b – Reacts to user actions by launching events
    on the server side
c – Reacts to user actions by launching events on
    the client side
d – Displays HTML
e – Is able to automatically posted back
f – Supports a RunAt=”Server” tag
g – The control has proprieties when used on a
    web page
Mid Term – Q3
Q. ASP.NET supports two types of Forms:
   HTML Forms and Web Forms. Name 2
   benefits of Web Forms over HTML
     Supports ViewState and PostBack
     Renders to the appropriate HTML at run
      time rather than “hard coded” html
Mid Term – Q4
Q. List 3 clear differences between HTML
   and XML. Give a total of 3 bullet points
     HTML is Flat, XML is hierarchical
     HTML has a fixed tag set, XML has user
      defined tags
     HTML represents a user interface and
      XML is used to represent data
Mid Term – Q5
Q. Give an example of XML that would be
   appropriate for a bookstore, with
   books and magazines. Books have
   authors, magazines have publishers.
   The bookstore has an address
<bookStore address=“123 mulberry”>
   <book name=“xyz” author=“abc”/>
   <magazine name=“xyz”
Mid Term – Q5
Alternative Answer example
   <address> 123 Mulberry street
Mid Term – Q6
Q. Give a class representation of the
   bookstore, books and magazine
   mentioned above. A bookstore has a
   set of books and a set of magazines,
   where a set is any type of collection
Public class Book
   public property Name as string
   public property Author as string
End Class
Mid Term – Q6
Public class Magazine
  public property Name as string
  public property Publisher as string
End Class

Public Class BookStore
  public property Books as Arraylist
  Public property Magazines as ArrayList
  public property Address as string
End class
Mid Term – Q7
Q. What is PostBack? Include ViewState
   in your explanation
   PostBack is the term used to refer to a
   WebForm‟s ability to post its form back
   to itself. This allows it to repopulate all
   controls with values stored in the
   ViewState, which is a hidden html input
   field in the form. This allows
   developers to avoid populating
   controls by hand.
Mid Term – Q8
Q. Give 2 different ways of that you can
   ensure that a piece of user information
   that is on page A is available to your
   page B You can assume that you‟re
   putting a LinkButton on page A that
   will include a redirection to Page B
   Response.Redirect with a query string
  Use sessions
Mid Term – Q9
Q. A junior developer has told you that he
   added an ArrayList to his WebForm1
   class as a data member. He has a
   button on WebForm1 that adds an item
   to the list and then displays it, but
   every time he hits the page the list is
   empty until he clicks a button to add,
   but if he refreshes the page its empty
   again. Why is this happening
Favorite Answer: “Why did my boss hire
A. WebForms are stateless, he‟s treating
   it as stateful. Should use a session.
Mid Term – Q10
Q. VB.NET is an object oriented language and so has
    the three attributes of an OO language:
    encapsulation, polymorphism and inheritance. Give
    an example of each of these three concepts
Encapsulation – See class example from Q6, the data
    members are encapsulated
Inheritance –
    class Parent
       public sub Boogie

   class Child inherits from Parent

   dim c as child
Mid Term – Q10
   dim p as parent
   p = new Child
Mid Term – Q11
Q. a) A querystring is used when searching through
   XML T / F (2)
 b) When a server control button is clicked, your handler runs but page load doesn’t
   because the page is already loaded T / F (2)
 c) In order to send MyAge=7 to page B, you can
   do the following
   T / F (2)
 d) Web.config is where we control whether the
   code is in debug or release compilation mode T
   / F - Invalidated
 e) Namespaces are optional in VB.NET (1) -
Mid Term – Q12
Q. Write a class with a property and a
   subroutine where the property can be used
   by anyone who instantiates an object of the
   class type (i.e. Dim item as new
   BookStoreItem()), and the subroutine is only
   able to be used by other methods within the
   same class or by its descendants, but not
   anyone who instantiates the class.
Public class Foo
   public property Name as string
   Protected sub Drive
End class
Mid Term – Q12
Q. Public class Sample
    Inherits SomeOtherClass
    private _someValue as integer
    Public Sub New()
        _someValue = 0
    End Sub
    Public Sub New(val as integer)
        _someValue = val
    End Sub
End Class
Public class ChildSample
    Inherits Sample
    Protected _someString as string
    Public Sub New(myVal as integer, myString as string)
    end Sub
End Class
Mid Term – Q13
a) If SomeOtherClass has a protected method, can
    ChildSample override it? (2)
       a – Yes
       b – No, it can only shadow it
       c – No, it can’t do anything with it
       d – it depends (on whether or not it has
    overrideable or not)
b) Sample has access to ChildSample._someString
       a – True
       b – False (because it is the parent of Child)
       c – it depends if they are in the same
       d – Only if it is cast as ChildSample
Mid Term – Q14
A. All users are using the application
   shopping cart, and it should be
   session shopping carts

A. Switch from application to session
   What‟s the difference between and
    exception and an error?
       Error is something that we except, an Exception
        isn‟t expected
       Exceptions all derive from System.Exception
       E.g. If the user didn‟t log in properly, is there an
        exception? No. If the user file you need to check
        for the security check isn‟t there or is corrupted,
        that‟s an exception
       ApplicationException is intended to be the one
        that you derive from for your application specific
        exceptions. Add your own properties for extra
   You can catch possible exception
    throwing code so that you can do
    something about it using
    Catch e as SqlException
        „ Do something
    Catch e as Exception
        „ Do something with this other type of exception
    End Try
   Try is used to wrap the code that could
    throw an exception
   Catch provides an opportunity to catch it
    and do something about it if an exception
    is thrown
   Finally is code that MUST be run whether
    or not there is an exception that is thrown,
    after all of the Try code has been run.
    Use this for things like making sure your
    database connection is closed
   The first exception matched is caught
   For example
    Catch e as Exception
        „ Do something
    Catch e as SqlException
        „ Do something with this other type of exception

   The first one is used even if it‟s a
   Throwing exceptions aren‟t expensive when
    you consider the situation
   Catching exceptions are expensive from a
    runtime perspective. Only catch them when
    you can really do something about it.
   Don‟t catch and rethrow unnecessarily
   You have a couple of other options for how
    to handle exceptions other than try/catch
       Page_Error event
       Application_Error event
       And an error page
MyPage.Error Event
   Webforms have an Error event that you
    can handle. This is intended for page
    specific logic to handle the problem
    and recover.
   But, the parameters for the event
    handler does not include the actual
    error, so we need to get it from the
    Server object. Server.GetLastError and
    then Server.ClearError
   See the code sample
   You can also set the error page for a
    specific page through its errorPage
    property or as a page directive
   If you use the page directive, you can
    change it at any time, including after its
    in production.
   You have to set CustomErrors to ON if
    you want these to work because it is
    defaulted to RemoteOnly
   See the code sample
   In global.asax there is a handler
    already there, but it‟s empty
   If you‟re page doesn‟t have an error
    page defined or a page.error event
    handler, it bubbles up to the
   You won‟t know what page the
    exception occurred on however,
    unless you threw your own exception
    and put it in
   <customErrors tag in web.config
   Can put in custom pages for standard
    HTTP Errors, e.g. 404
<customErrors mode=“RemoteOnly”
    <error statusCode=“404”
Another feature
   The Server object provides two other
    ways of moving a user to a specific
    page (i.e. instead of only using
   Server.Transfer
       Allows …
   Server.Execute
       Allows …
How to prevent some errors
   Validation Controls
       Range Validators
         Only values btwn X and Y allowed
         but doesn‟t make the field required
       Compare Validators
         Compares values in two different
          controls or against a specific value
       RequiredField Validator
       RegularExpression Validator
How to prevent some errors
   Validation Controls (cont‟d)
       Summary Validator
         Centralizes all of our messages back to the
         It uses the Text property of the controls vs
            Error Message property is displayed wherever
            the individual validation control is placed
       Some validators check before submission
        attempted, others only kick in when the user
        tries to submit
       If any validator fails, the form doesn‟t submit
Finding the errors
   We‟ve talked briefly about Trace and Debug
    before, but now let‟s really look at it
   There are two facilities that you can take
    advantage of in ASP.NET
       Debug
         Cool features include Debug.WriteIf IF!!
         Removed from your code when you compile
           as release code
       Trace
         Always in your code and able to be turned
           on/off in either the page directive (in your
           aspx file) , or in the web.config
         Trace code is always run but isn‟t necessarily
           displayed. So sometimes you need to check,
           if Trace.Enabled then …
Finding the errors
   Unfortunately the WebForm layout is
    affected. It looks fine with Flow layout, but
    WebForms by default use Grid
   The difference
       GridLayout – specifies where on the screen the
        control is to be
       FlowLayout – just uses standard HTML to
   Trace on the GridLayout looks really gross if
    pageOutput = true (next slide)
   Otherwise use pageOutput = false
Finding the errors
<trace enabled="false" requestLimit="10"
   localOnly="true" /> in web.config
 Enabled = true then all your trace.Write
   for all pages will show
 pageOutput = True puts all of the info
   at the “Bottom” of your page. If False,
   then a virtual page is created, trace.axd
 traceMode = sort order

Description: programming language c sharp ,learn c# by slides