ajaysubramanicareer
5 views
22 slides
Aug 30, 2025
Slide 1 of 22
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
About This Presentation
Java Programing - Exceptional Handling Concepts
Size: 7.7 MB
Language: en
Added: Aug 30, 2025
Slides: 22 pages
Slide Content
EXCEPTION HANDLING IN JAVA Presented by Team 5 S Aakash 310623104002 Ajay S 310623104009 Akash A 310623104010 Chezhian V 310623104032 Jerome Jestin A 310623104060 231CSC404T- JAVA PROGRAMING 1
What is Exception Handling and why do we need it Exception handling is a programming mechanism that detects, manages, and responds to runtime errors to prevent application crashes. It uses constructs like try, catch, and finally to gracefully handle unexpected situations. Why Do We Need It? Prevents Application Crashes Improves User Experience Enables Error Recovery Enhances Debugging & Maintenance Ensures Data Integrity 2
Types of Exceptions Known at compile-time. Example: IOException , SQLException . Checked Exceptions Unchecked Exceptions Occur during runtime. Example: NullPointerException , ArrayIndexOutOfBoundsException . Critical issues beyond program control. Example: StackOverflowError , OutOfMemoryError Errors 3
TRY-CATCH Basic try-catch 4
2. Multiple catch 5
3. Single catch - Multiple Exception 6
4. Nested try-catch 7
5. try-catch-finally 8
In java we have already defined exception classes Eg: ArithmeticException, NullPointerException etc. The throw keyword is used to explicitly throw an exception. These exceptions are known as user-defined exceptions. Syntax: throw new MyException(); Throwing our Own Exceptions throw keyword 9
•The throws keyword is used to declare an exception. •It gives an information to the programmer that there may occur an exception. •So it is better for the programmer to provide the exception handling code so that normal flow can be maintained. throws keyword Syntax of throws keyword: void method_name() throws exception_class_name { ... } 10
class Example { public static void checkAge(int age) throws Exception { if (age < 18) { throw new Exception("Age is less than 18, not eligible."); } else { System.out.println("Age is valid."); } } public static void main(String[] args) { try { checkAge(15); } catch (Exception e) { System.out.println("Exception caught: " + e.getMessage()); } } } 11
Custom exceptions extend Exception class, used for specific errors in business logic. Requires constructor and super(message) for messages. class CustomException extends Exception { public CustomException(String message) { super(message); } } public class Main { public static void main(String[] args) { try { throw new CustomException("This is a custom exception"); } catch (CustomException e) { System.out.println(e.getMessage()); } } } Custom Exception 12
Efficient Exception Handling Catch specific errors : Try to catch specific errors (like FileNotFoundError ) instead of a general one (Exception) so you know exactly what went wrong. Only handle known issues : If you know how to fix a problem, catch that specific error. Otherwise, let it go up so it can be handled at a higher level (like at the start of your program). Don’t ignore errors : Avoid using empty except blocks because they hide errors. At least log the error or handle it properly so you know something went wrong. Use finally for cleanup : If you need to close files or clean up resources, use finally so that it happens no matter what (whether an error occurred or not). 13
Banking Use Case: Insufficient Balance Exception 14
E-Commerce Use Case: Product Not Available Exception 15
Healthcare Use Case: Invalid Patient Age Exception 16
conclusion Exception handling in Java ensures smooth program execution by catching and handling runtime errors. It improves code reliability, maintains flow control, and prevents crashes. Using try, catch, finally, and throw, Java handles errors efficiently for robust applications.. 17
"Let's get to the questions" 18
1. What is an exception in programming? a) A syntax error b) A runtime error c) A hardware issue d) A virus 2. Which keyword is used to handle exceptions in Java? a) catch b) try c) handle d) error Questions 19
3. What happens if an exception is not handled? a) The program continues b) The program crashes c) The program runs faster d) The program fixes itself 4. Which block must always be used with try? a) finally b) except c) catch d) raise Questions 20
5. What does the finally block do? a) Runs only if no exception occurs b) Runs always, after try/except c) Skips execution d) Stops the program Questions 21