0% found this document useful (0 votes)
31 views25 pages

AP M24 Week4

adv programing

Uploaded by

aradhyabaswal01
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views25 pages

AP M24 Week4

adv programing

Uploaded by

aradhyabaswal01
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 25

Advanced Programming

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

Accessible with an object of


the outer class in the inner.
Inner Classes

Accessible without requiring


in object. Outer class
encapsulates the inner class
as well.
Anonymous
Class
Exceptions and Exception Handling
OOP avatar of errors and error handling.

- Array out of bounds.


- IOException – e.g. File not found error.
- Mathematical exception – divide by zero error, NAN error,
floating point exception etc.
Being Defensive is Important

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

● try denotes code that may throw an exception


o place questionable code within a try block
o a try block must be immediately followed by a catch block unlike an if w/o else thus,
o try-catch blocks always occurs as pairs

● 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.

How JVM handles exceptons:


1. Look for exception handler in current stack frame (method)

2. If not found, then terminate the execution of current method and go


to the callee method and repeat step 1 by looking into callee’s exception
table

3. If no matching handler is found in any stack frame, then JVM finally


terminates by throwing the stack trace (printStackTrace method)
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

● try denotes code that may throw an exception


o place questionable code within a try block
o a try block must be immediately followed by a catch block unlike an if w/o else thus,
o try-catch blocks always occurs as pairs

● 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

In try {} code after the


throw is skipped.

The program jumps to the


catch() handler.

If no appropriate handler,
then JRE handles it and
show it on stdout
Catching What Was Thrown

Combining exceptions
Rethrowing Exceptions
Finally Clause

- Executed at the end of all try{} catch{} blocks.

- Last set of instructions to be called before the program


terminates. Usually used for resource release and clearup
operations.
Finally Clause
Using Assertions
- Idiomatic tools for defensive programming.
- Faster execution than throwing exceptions.
- Not to be used for everyday programs.
- Irrecoverable.
- Usually program terminates.

assert <condition>;

assert <condition> : <expression> ;

[ Check condition. If false then create an object with argument


<experession> of type AssertionError – JVM catches it and prints the details
presented in the <expression> ]
Using Assertions
if (x > 0){
.... if (x > 0){ assert x>0 : new String(“x>0”);
} throw new
Else { exception(“myexception”) ;
...
}
}

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.

SEVERE, WARNING and INFO


are always enable for every
Java program by default.
Logging Unexpected Exceptions
Two methods commonly used:

You might also like