0% found this document useful (0 votes)
7 views

Lec 09- Exception Handlings

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Lec 09- Exception Handlings

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

CS SE 1209 & IT 1210

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.

• It cannot be caught or handled.

• It indicates a serious problem.

• It occurs at run time.

• These are always unchecked.

• An example of errors is OutOfMemoryError, LinkageError, AssertionError, etc. are the


subclasses of the Error class.

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

Runtime errors occur during the execution of the Java program.

These errors are not typically caught by the compiler but can cause the program to terminate abnormally or exhibit
unexpected behavior at runtime.

Common examples include:

• ArrayIndexOutOfBoundsException: Occurs when attempting to access an array element at an index that is


outside the bounds of the array.

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.

• There are two types of exceptions i.e. checked and unchecked.

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

Purpose of try Block-

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.

• ExceptionType: The type of exception that the catch block is designed to


handle. It can be a specific exception class (like IOException,
NullPointerException, etc.) or a superclass (like Exception or Throwable).

• e (Exception Parameter): Represents the exception object that provides


information about the thrown exception, such as its message or stack
trace.

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.

• IOException: It is thrown when an input-output operation failed or interrupted

• NoSuchFieldException: It is thrown when a class does not contain the field (or variable) specified

• NoSuchMethodException: It is thrown when accessing a method that is not found.

• NullPointerException: This exception is raised when referring to the members of a null object. Null represents
nothing

• RuntimeException: This represents an exception that occurs during runtime.


19
T.L.Navodi Sithara Saegis Campus
Multiple Catch Block(Example)
Multiple catch blocks in Java are used to handle different types of exceptions that may occur within a
try block.

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:

• It’s placed after the last catch block.

• 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

You might also like