Exceptions, Singleton, Eworld - PowerPoint PPT Presentation

About This Presentation
Title:

Exceptions, Singleton, Eworld

Description:

{ System.err.println( 'Exception while running DepthFirstTraversal ' e. ... Just rethrow it. throw new DatabaseException(e, 'while getting column descriptors' ... – PowerPoint PPT presentation

Number of Views:29
Avg rating:3.0/5.0
Slides: 16
Provided by: Alexi79
Category:

less

Transcript and Presenter's Notes

Title: Exceptions, Singleton, Eworld


1
Lecture 4
  • Exceptions, Singleton, Eworld XML

2
Error handling in procedural languages
  • int foo()
  • if ( !doSomething1() )
  • return 1
  • if ( !doSomething2() )
  • return 2

3
Exceptions
  • public interface IGraphAlgorithm
  • public void apply( IGraph ) throws
    ExecutionException

4
Example
  • IGraph graph BasicGraph()
  • IGraphAlgorithm alg
  • new DepthFirstTraversal()
  • try
  • alg.apply( graph )
  • catch ( ExecutionException e )
  • System.err.println( "Exception while running
    DepthFirstTraversal e.getMessage() )
  • e.printStackTrace()

5
Creating your own exception class
  • public class DatabaseException extends Exception
  • public DatabaseException(String message )
  • super( message )

6
Using exceptions throughout your package
  • public class TableDescriptor
  • public Enumeration getColumnDescriptors()
  • throws DatabaseException
  • try
  • // do whatever
  • catch ( Exception e )
  • // Just rethrow it
  • throw new DatabaseException(e, while getting
    column descriptors )

7
Wrapping exceptions
  • public interface IExceptionWrapper
  • public Exception getException()
  • public class DatabaseException
  • extends Exception implements
    IExeptionWrapper
  • public DatabaseException( String message )
    super( message )
  • public DatabaseException( String message,
    Exception eWrapped )
  • super( message )
  • this.eWrapped eWrapped
  • public Exception getException() return
    eWrapped
  • private Exception eWrapped

8
Catching different exceptions
  • try
  • // Block of code
  • catch ( ExceptionTypeOne e1 )
  • // do something
  • catch ( ExceptionTypeOne e2 )
  • // do something
  • catch ( ExceptionTypeN eN )
  • // do something
  • catch ( Exception e ) // Should always be
    last
  • // do something

9
Finally clause
  • public class StreamTest
  • public static void main( String args )
  • OutputStream os null
  • try
  • os new DataOutputStream( args.txt )
  • for ( int i 0 i lt args.length i )
  • os.println( ARG i args i
    )
  • catch ( IOException e )
  • System.err.println( Exception while writing
    to stream e.getMessage() )
  • finally
  • if ( os ! null )
  • try os.close() catch ( Exception e )

10
Singleton
  • public class Singleton
  • public static Singleton getInstance()
  • return instance
  • private Singleton()
  • // create instance
  • private static Singleton instance new
    Singleton()
  • public static void main( String args )
  • Singleton handle1 Singleton.getInstanc
    e()
  • Singleton handle2 Singleton.getInstance
    ()
  • System.out.println( "Handle1 " handle1
    "" )
  • System.out.println( "Handle2 "
    handle2 "" )
  • Handle1 Singleton_at_1c9f7e
  • Handle2 Singleton_at_1c9f7e

11
Static initialization blocks
  • static
  • Singleton mySingleton
  • new Singleton()
  • mySingleton.init( "Hello" )
  • static
  • int iArray new int 100
  • for ( int i 0 i lt iArray.length i )
  • iArray i i

12
EWorld
Fighting










Lunch
E
E
r
E
E
Trading
r
E
E
E
E
Clone
Mating
E
r
r
13
XML
  • XML is a meta language
  • Every tag may have attributes and children
  • ltemailgt
  • ltfromgtAlex Iskoldlt/fromgt
  • lttogtStudents of SE Java, 2001lt/fromgt
  • ltmessagegt
  • XML does not mix syntax semantics!
  • lt/messagegt
  • lt/emailgt

14
But in HTML
  • lthtmlgt
  • ltbodygt
  • lth1gtFrom Alex Iskoldlt/h1gt
  • lth1gtTo Students of SE Java, 2001lt/h1gt
  • ltbgtXML does not mix syntax
    semantics!lt/bgt
  • lt/bodygt
  • lt/htmlgt

15
Assignment
  • Please read
  • Arnold Gosling Java Programming language
    Chapter 7, 12
  • Singleton, page 127 Design Patterns book
Write a Comment
User Comments (0)
About PowerShow.com