Lec 09- Exception Handlings
Lec 09- Exception Handlings
Lecture 09
Exception Handlings
23/07/2024
Bachelor of Science (Hons) in Computer Science | Software Engineering | Information Technology
Department of Computing
Faculty of Computing and Technology
Saegis Campus
Nugegoda
1
T.L.Navodi Sithara Saegis Campus
Errors and Exceptions
2
T.L.Navodi Sithara Saegis Campus
Errors and Exceptions
3
T.L.Navodi Sithara Saegis Campus
Errors
• Errors are problems that mainly occur due to the lack of system resources.
4
T.L.Navodi Sithara Saegis Campus
Types of Errors
1. Compile-Time Errors
Compile-time errors, also known as syntax errors, occur when the Java compiler encounters code that violates the
language rules.
These errors prevent the compiler from translating the source code into bytecode. Common examples include:
• Syntax Errors: Mistakes in the syntax of the Java language. For instance, missing semicolons, mismatched parentheses,
or incorrect use of keywords.
5
T.L.Navodi Sithara Saegis Campus
Types of Errors
• Type Errors: Operations that are performed on incompatible types. For example, assigning a string to an integer variable
without proper casting.
• Semantic Errors: Logical errors in the program that result in incorrect behavior. These errors might not be caught by the
compiler but can lead to unexpected outcomes during runtime.
6
T.L.Navodi Sithara Saegis Campus
Types of Errors
2. Runtime Errors
These errors are not typically caught by the compiler but can cause the program to terminate abnormally or exhibit
unexpected behavior at runtime.
7
T.L.Navodi Sithara Saegis Campus
Types of Errors
• ArithmeticException: Occurs when dividing by zero or performing an illegal arithmetic operation.
• OutOfMemoryError: Occurs when the Java Virtual Machine (JVM) runs out of memory, typically due to
excessive allocation of objects.
8
T.L.Navodi Sithara Saegis Campus
Exceptions
• The term exception is shorthand for the phrase exception event.
• It is an event that occurs during the execution of the program and interrupts the normal flow of program
instructions.
• These are the errors that occur at compile time and run time.
• It occurs in the code written by the developers. It can be recovered by using the try-catch block and throws
keyword.
• When an exception occurs within a method, it creates an object. This object is called the exception object.
• It contains information about the exception such as the name and description of the exception and state of
the program when the exception occurred.
9
T.L.Navodi Sithara Saegis Campus
Error classes and Exception Classes in Java
10
T.L.Navodi Sithara Saegis Campus
Simple Example
11
T.L.Navodi Sithara Saegis Campus
Try-Catch-Finally
12
T.L.Navodi Sithara Saegis Campus
Try
• The try block in Java is used to enclose a block of code where exceptions (errors) might occur
during the execution of a program.
Exception Handling: The primary purpose of the try block is to identify a block of code where exceptions may occur
The try block starts with the keyword try followed by a set of statements enclosed in curly braces {}.
• If an exception occurs within the try block, the flow of control immediately jumps to the catch block.
• The catch block specifies the type of exception (ExceptionType) that it can handle and provides code to handle the
exception, such as displaying an error message or logging the exception.
13
T.L.Navodi Sithara Saegis Campus
Try Block
14
T.L.Navodi Sithara Saegis Campus
Catch Block
The catch statement allows you to define a block of code to be executed, if an error occurs in the try
block.
(The primary purpose of the catch block is to handle exceptions that are thrown within a
corresponding try block.)
15
T.L.Navodi Sithara Saegis Campus
Catch Block
• try Block: Encloses the code where exceptions may occur.
• catch Block: Follows the try block and specifies the type of exception
(ExceptionType) it can handle.
16
T.L.Navodi Sithara Saegis Campus
Catch Block(Example)
17
T.L.Navodi Sithara Saegis Campus
Catch Block(Example)
18
T.L.Navodi Sithara Saegis Campus
Catch Block(Example)
• ArithmeticException: It is thrown when an exceptional condition has occurred in an arithmetic operation.
• ArrayIndexOutOfBoundsException: It is thrown to indicate that an array has been accessed with an illegal index. The
index is either negative or greater than or equal to the size of the array.
• ClassNotFoundException: This Exception is raised when we try to access a class whose definition is not found
• FileNotFoundException: This Exception is raised when a file is not accessible or does not open.
• NoSuchFieldException: It is thrown when a class does not contain the field (or variable) specified
• NullPointerException: This exception is raised when referring to the members of a null object. Null represents
nothing
20
T.L.Navodi Sithara Saegis Campus
Multiple Catch Block(Example)
21
T.L.Navodi Sithara Saegis Campus
Finally Block
• The finally statement lets you execute code, after try...catch, regardless of the result:
• If there are no catch blocks, the finally block immediately follows the try block.
• A finally block of code always executes, whether or not an exception has occurred.
finally {
// this block always executes
}
22
T.L.Navodi Sithara Saegis Campus
Throwing User Defined Exception
23
T.L.Navodi Sithara Saegis Campus
24
Lecturer Name Saegis Campus