0% found this document useful (0 votes)
46 views36 pages

Comprog2 Week8 - Session 1-2

This document provides an overview of exception handling in Java programming. It defines exceptions as events that interrupt normal program flow, such as errors. Exception handling allows programs to gracefully deal with errors using try, catch, and finally blocks. The document outlines different types of exceptions, how to generate and throw exceptions, and how to handle exceptions to maintain program flow. Key aspects covered include checked vs unchecked exceptions, the throws clause, and examples of try, catch, and finally blocks.

Uploaded by

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

Comprog2 Week8 - Session 1-2

This document provides an overview of exception handling in Java programming. It defines exceptions as events that interrupt normal program flow, such as errors. Exception handling allows programs to gracefully deal with errors using try, catch, and finally blocks. The document outlines different types of exceptions, how to generate and throw exceptions, and how to handle exceptions to maintain program flow. Key aspects covered include checked vs unchecked exceptions, the throws clause, and examples of try, catch, and finally blocks.

Uploaded by

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

AY 2021-2022

COMPUTER
PROGRAMMING 2
Prepared By:

Prof. Jomariss B. Plan, MSIT


CCS Faculty
TOPIC:
EXCEPTION
HANDLING
Learning Outcomes:
At the end of the topic session, the students
should be able to:
1. Explain the concept of exception.
2. Construct exceptions in Java programming.
3. Apply try, catch, and finally blocks.
4. Apply throws clause.
WHAT IS EXCEPTION

It is an event, usually some form of error,


which happens during the normal course of
program execution.

In java, an exception is an event that


interrupts the normal flow of the program.
WHAT IS EXCEPTION HANDLING
This is a technique to manage such errors
comprises the group of methods.

Exception Handling is one of the most


powerful mechanism to handle the runtime
errors so that the normal flow of the
application can be maintained
GENERATING EXCEPTIONS

 Method Calls
- If a method throws as exception, then calling
that method may result in an exception.
GENERATING EXCEPTIONS

 Runtime Exceptions
- Can occur even though the offending piece
of code does not declare that it throws an
exception.
TYPES OF EXCEPTIONS

 Checked Exceptions
 Unchecked Exceptions
TYPES OF EXCEPTIONS

 Checked Exceptions
- The classes which directly inherit
Throwable class except RuntimeException
and Error are known as checked
Exceptions e.g. IOException,
SQLException etc.
- Checked Exceptions are checked at
compile time.
TYPES OF EXCEPTIONS

 Unchecked Exceptions
- The classes which inherit
RuntimeException are known as
unchecked Exceptions e.g.
NullPointerExceptions,
ArrayIndexOutOfBoundsException etc.
- Unchecked Exceptions are checked at
runtime.
THE THROWS CLAUSE

 The throws clause lists the types of


exceptions that a method might throw.

 General format:
THE THROWS CLAUSE- example
HANDLING EXCEPTIONS

 The try Block


- Block of code you attempt to execute while
acknowledging that an exception might
occur.
- Consist of the following format:
HANDLING EXCEPTIONS
 The catch Block
- Segment of code that can handle an exception that
might be thrown by the try block that precedes it.
- Consist of the following format:
Try catch- example
class ArithmeticException_Demo
{
public static void main(String args[])
{
int a = 30, b = 0;
int c = a/b; // cannot divide by zero
System.out.println ("Result = " + c);
}
}}
Output:
Try catch- example
class ArithmeticException_Demo
{
public static void main(String args[])
{
try {
int a = 30, b = 0;
int c = a/b; // cannot divide by zero
System.out.println ("Result = " + c);
}
catch(ArithmeticException e) {
System.out.println ("Can't divide a number by 0");
}
}}
Output:
HANDLING EXCEPTIONS
 The finally Block
- When you have actions you must perform at the
end of the a try… catch blocks
THINGS TO REMEMBER

1. The block notation is mandatory.


2. For each try block, there can be one or more catch
blocks, but at most only one finally block.
3. The catch blocks and finally blocks must always
appear in conjunction with the try block, and in the
above order.
4. A try block must be followed by at least one catch
block or one finally block.
More Examples
class GFG {
public static void main (String[] args) {
int[] arr = new int[4];
try
{
int i = arr[4];
System.out.println("Inside try block");
}
catch(ArrayIndexOutOfBoundsException ex)
{
System.out.println("Exception caught in Catch block");
}
System.out.println("Outside try-catch clause");
}
}
Output
More Examples
Output

Exception caught in catch block


finally block executed
Outside try-catch-finally clause
Video Clip Presentations

YouTube Vlog Channel


Title: Exception_Handling_in_Java_-_Javatpoint
https://www.youtube.com/watch?v=ohpCMpd
erow
Title: Java Programming Tutorial - 82 -
Exception Handling
https://www.youtube.com/watch?v=K_-
3OLkXkzY
That ends our Week 8 Session 1-2!

NEXT MEETING--

WEEK 10 SESSION 1:
GUI (Swing Components)
REFERENCES:
 https://www.baeldung.com/java-
common-exceptions
 https://www.geeksforgeeks.org/types-
of-exception-in-java-with-examples/
 https://www.javatpoint.com/exception-
handling-in-java

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy