AP M24 Week4
AP M24 Week4
CSE 201
Instructor: Sambuddho
(Semester: Monsoon 2024)
Week 4 – Inner Classes
and Exceptions/Exception
Handling
Inner Classes
Inner Classes
Implicit reference to object
of outer class. However the
correct way to use is to refer
with object of the outer
class.
Inner Classes
8
© Vivek Kumar
Exception Handling Syntax
● Process for handling exceptions
o try some code, catch exception thrown by tried code, finally, “clean up” if necessary
o try, catch, and finally are reserved words
● catch exception thrown in try block and write special code to handle it
o catch blocks distinguished by type of exception
o can have several catch blocks, each specifying a particular type of exception Once an
o exception is handled, execution continues after the catch block
● finally (optional)
o special block of code that is executed whether or not an exception is thrown
o follows catch block 9
Andries van Dam © 2016 9/22/16
Exceptions and Exception Handling
Exceptions
are also
classes
Exceptions Handling by JVM
Any method invocdation is represented as a “stack frame” on the
Java “stack”
Callee-Caller relationship: If method A calls method B then A is caller and
B is callee
Each frame stores local variables, input parameters, return values
and intermediate calculations
In addition, each frame also stores an “exception table”
This exception table stores information on each try/catch/finally block, i.e.
the instruction offset where the catch/finally blocks are defined.
● catch exception thrown in try block and write special code to handle it
o catch blocks distinguished by type of exception
o can have several catch blocks, each specifying a particular type of exception Once an
o exception is handled, execution continues after the catch block
● finally (optional)
o special block of code that is executed whether or not an exception is thrown
o follows catch block 1
Andries van Dam © 2016 9/22/16
2
Exceptions and Exception Handling
Methods tells Java compiler that what kind of errors it can
throw.
Throwing an Exception
Creating and Throwing Exception Class
Catching What Was Thrown
If no appropriate handler,
then JRE handles it and
show it on stdout
Catching What Was Thrown
Combining exceptions
Rethrowing Exceptions
Finally Clause
assert <condition>;
catch(Exception ep){
...
}
Logging
- System.out.println() cannot be always used – makes things slow.
- Adding and removing them at all places can be cumbersome.
- Usually not used in production code.
- Logging can be collectively enabled or supressed.
Log Levels
logger.warning(msg);
logger.fine(msg);
Top -> Bottom levels of logging.
logger.log(Level.<Levelnam
If you log a bottom level, e>, msg);
then you log all levels above it.