Exceptions, Singleton, - PowerPoint PPT Presentation

1 / 26
About This Presentation
Title:

Exceptions, Singleton,

Description:

... void setPrinter( ILogPrinter lp ); public static ILogPrinter ... Arnold & Gosling Java Programming language Chapters on Exception and Garbage collection ... – PowerPoint PPT presentation

Number of Views:35
Avg rating:3.0/5.0
Slides: 27
Provided by: Alexi79
Learn more at: https://cs.nyu.edu
Category:

less

Transcript and Presenter's Notes

Title: Exceptions, Singleton,


1
Lecture 3
  • Exceptions, Singleton,
  • Bridge Logging

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

3
Exceptions
  • public interface Algorithm
  • public void run() throws
    ExecutionException, IllegalDataException

4
Example
  • int iArray randomSortedIntArray()
  • IAlgorithm alg new BinarySearch( iArray, 29 )
  • try
  • alg.run()
  • catch ( IllegalDataException e1 )
  • System.err.println( "Illegal Data Exception
    while running Binary Search " e.getMessage()
    )
  • e.printStackTrace()
  • catch ( ExecutionException e )
  • System.err.println( "Execution Exception while
    running Binary Search " e.getMessage() )
  • e.printStackTrace()

5
How the catch block works
  • if ( thrown exception instance of
    IllegalDataException )
  • else if ( thrown exception instanceof
    ExecutionException )

6
printStackTrace method
  • java.io.FileNotFoundException fred.txt
  • at java.io.FileInputStream.ltinitgt(FileInpu
    tStream.java)
  • at java.io.FileInputStream.ltinitgt(FileInpu
    tStream.java)
  • at ExTest.readMyFile(ExTest.java19)
  • at ExTest.main(ExTest.java7)

7
Heap/Stack
8
Swallowing exceptions is is a major NO-NO!
try anObject.method( arg1 ) catch (
Exception e )
9
(No Transcript)
10
Runtime error 3256 at 0x34AD546
11
Exceptions allow for extra info
  • public class ParserException extends Exception
  • public ParserException( File f, int iLine )
  • this.f f
  • this.iLine iLine
  • public final File getFile() return f
  • public final int getLine() return iLine
  • private File f
  • private int iLine

12
Creating your own exception classes
  • public class ApplicationException
  • extends Exception
  • public class ApplicationOpenFailedException
  • extends ApplicationException

13
Throwing multiple exceptions
  • public interface IApplication
  • public void open() throws ApplicationOpenFailedE
    xception, IOException
  • public void close() throws ApplicationCloseFaile
    dException, IOException

14
finally
  • public class AlwaysDoThatWithStreams
  • public static void main( String args )
  • FileWriter fw null
  • try
  • fw new FileWriter( "args.txt" )
  • for ( int i 0 i lt args.length i )
  • os.println( "ARG" i " "
  • args i "" )
  • catch ( IOException e )
  • System.out.println( "Error while writing
    args " e.getMessage() )
  • finally
  • if ( fw ! null ) fw.close()

15
public static int parseInt(String s,
int radix) throws NumberFormatException
16
Logging
  • 01/01/2002 94503 EntityReader ERROR class
    StrangeEntity not found
  • 01/01/2002 94505 EntityReader DEBUG created
    instance of BasicEntity
  • Log.error( this, "class StrangeEntity not found"
    )
  • Log.debug( this, "created instance of
    BasicEntity" )

17
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

18
Log class
  • public final class Log
  • public static void debug( String msg )
  • public static void debug( Object this, String
    msg )
  • public static void error( String msg )
  • public static void error( Object this, String
    msg )
  • public static void warning( String msg )
  • public static void warning( Object this,
    String msg )
  • public static void setOptionsOn( int options
    )
  • public static void setOptionsOff( int options
    )
  • public static void setPrinter( ILogPrinter lp
    )
  • public static ILogPrinter getPrinter()
  • public static void registerClass( Class c )
  • public static void unregisterClass( Class c
    )

19
Log as utility class
  • Log.getInstance().error( "Something is wrong" )
  • Log.error( "Something is wrong" )

20
Basic Log methods
  • public final class Log
  • public static void error( String msg )
  • public static void error( Object o, String msg
    )
  • public static void debug( String msg )
  • public static void debug( Object o, String msg
    )

21
Passing the object whichreports the message
  • public static void error( Object o, String msg )
  • Log( "ERROR " o.getClass().getName()
  • "" msg )

22
Factoring out the implementation
  • public class ILogImpl
  • public void print( String sMessage )

23
Factoring out the implementation
  • public class Log
  • public static void setLogImpl( ILogImpl lm )
  • logImpl lm
  • private void print( String s )
  • logImpl.print( s )
  • private static ILogImpl logImpl

24
StdOutLogImpl
  • public final class StdOutLogImpl implements
    ILogImpl
  • public StdOutLogImpl ()
  • public void print( String s )
  • System.out.println( s )

25
Complete Log class
  • public class Log
  • /
  • _at_param Object o - the object, which
    signals the error
  • _at_param String sMsg - the error message
  • _at_param Throwable - the exception which
    occurred
  • /
  • public static void error( Object o, String
    sMsg, Throwable e )
  • public static void error( String sMsg,
    Throwable e )
  • public static void error( Object o, String
    sMsg )
  • public static void error( String sMsg )
  • public static void debug( Object o, String
    sMsg )
  • public static void debug( String sMsg )
  • public static void warning( Object o, String
    sMsg )
  • public static void warning( String sMsg )
  • /
  • _at_param Boolean bDebug - indicates if the
    debugging messages

26
Assignment
  • Please read
  • Arnold Gosling Java Programming language
    Chapters on Exception and Garbage collection
  • Chapters 16 and 25 from Agile Software
    Development Book
  • Assignment 1
  • Define implement Log, ILogImpl, StdOutLogImpl,
    FileLogImpl, MultiLogImpl
Write a Comment
User Comments (0)
About PowerShow.com