Handling ugly page crashes
This presentation is the property of its rightful owner.
Sponsored Links
1 / 12

Handling Ugly Page Crashes PowerPoint PPT Presentation


  • 47 Views
  • Uploaded on
  • Presentation posted in: General

Handling Ugly Page Crashes. With Try - Catch. Handling Ugly Page Crashes. Page crashes are ugly Syntactically correct code also fails TRY-CATCH helps recovery Use TRY-CATCH while developing web pages TRY contains the risky code CATCH contains recovery code

Download Presentation

Handling Ugly Page Crashes

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Handling ugly page crashes

Handling Ugly Page Crashes

With Try - Catch


Handling ugly page crashes1

Handling Ugly Page Crashes

  • Page crashes are ugly

  • Syntactically correct code also fails

  • TRY-CATCH helps recovery

  • Use TRY-CATCH while developing web pages

    • TRY contains the risky code

    • CATCH contains recovery code

    • FINALLY contains code that must always execute


Try catch logic

TRY-CATCH Logic


What it looks like

What it looks like

TRY to run this

Try

Catch (Exception Ex)

Finally

{

there are lines of code here;

That have some chance of crashing;

}

No Error?

Run FINALLY anyway

Error?

Then CATCH it here

Continue on

Run Finally

{

List the error message;

and any other recovery steps;

}

{

What must always be run;

Comes in these lines of code;

}


Syntactically perfect pages also fail

The first checkbox

Syntactically perfect pages also fail

1. Customer Registration page

3. First value is #0

4. The last value is #3

So the code loops 4 times

The syntax is good.

No 4th checkbox  Logic fails.

This is a RUNTIME error.

2. There are 3 checkboxes in the list


Page with error without try catch

Page with error WITHOUT Try-Catch


Page with error with try catch

Page with error WITH TRY-CATCH

1. No ugly yellow screen with crazy stuff

2. Error message appears nicely

3. The page actually finishes running

Results are in the box!


How to write a try catch

How to write a TRY-CATCH

  • Potentially risky code

  • It in a TRY block

  • It with a CATCH block just under it

  • Add a FINALLY block just under that

SELECT

SURROUND

SUPPORT

SOMETIMES


For example

For example

Try {

// sample code here

// that has potential to fail

}

Catch (Exception Ex) {

// do this when the code above fails

// say what the code in the try block was doing

// and what (if you know) the error could be

}

Finally {

// always do this stuff

// such as closing readers,

// or even connections if you are done with the database

}


Circled code has been added

Circled code has been added

]

This code could fail

]

If it fails, then run this code to show what the error message is

lblErrors.Text=Ex.Message.ToString();


Can you tell what has been added

Can you tell what has been added?


Some places to use try catch

Some places to use Try-Catch

  • Loops, especially where one hard codes counters

  • While converting between data types

  • While executing commands, either with DataReaders, or ExecuteNonQueries


  • Login