Title: Exceptions, Singleton,
1Lecture 3
- Exceptions, Singleton,
- Bridge Logging
2Error handling in procedural languages
- int foo()
- if ( !doSomething1() )
- return 1
-
- if ( !doSomething2() )
- return 2
-
-
3Exceptions
- public interface Algorithm
- public void run() throws
ExecutionException, IllegalDataException
4Example
- 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()
5How the catch block works
- if ( thrown exception instance of
IllegalDataException ) -
- else if ( thrown exception instanceof
ExecutionException ) -
6printStackTrace 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)
7Heap/Stack
8Swallowing exceptions is is a major NO-NO!
try anObject.method( arg1 ) catch (
Exception e )
9(No Transcript)
10Runtime error 3256 at 0x34AD546
11Exceptions 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
12Creating your own exception classes
- public class ApplicationException
- extends Exception
- public class ApplicationOpenFailedException
- extends ApplicationException
13Throwing multiple exceptions
- public interface IApplication
- public void open() throws ApplicationOpenFailedE
xception, IOException - public void close() throws ApplicationCloseFaile
dException, IOException
14finally
- 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()
-
-
15public static int parseInt(String s,
int radix) throws NumberFormatException
16Logging
- 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" )
17Singleton
- 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
18Log 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
) -
19Log as utility class
- Log.getInstance().error( "Something is wrong" )
- Log.error( "Something is wrong" )
20Basic 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
)
21Passing the object whichreports the message
- public static void error( Object o, String msg )
- Log( "ERROR " o.getClass().getName()
- "" msg )
22Factoring out the implementation
- public class ILogImpl
- public void print( String sMessage )
-
23Factoring 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
24StdOutLogImpl
- public final class StdOutLogImpl implements
ILogImpl - public StdOutLogImpl ()
- public void print( String s )
- System.out.println( s )
-
-
25Complete 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
26Assignment
- 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