Exception Handling
Exception Handling
Presentation by
Mrs.S.Janani
Assistant Professor
Department of Information Technology
[email protected]
Department of
IT
Information
Technology
Drsnsrcas/CPreprocessor/April 27, 2024 Slide No : 1
INTRODUCTION
• Due to design errors or coding errors, our programs may fail in
unexpected ways during execution. An exception is a condition that
is caused by run time error in the program. The purpose of the
exception handling mechanism is to provide a means to detect and
report an “ecxceptional circumstances” .
Department of
www.snsgroup.com
IT Information
Technology
Drsnsrcas/CPreprocessor/April 27, 2024 Slide No : 2
An error may produce an incorrect output or may terminate
the execution of the program abruptly or even may cause
the system to crash. So it is our responsibility to detect and
manage the error properly.
Department of
www.snsgroup.com
IT
Information
Technology
Drsnsrcas/April 27, 2024 Slide No : 3
Types of Error
Runtime Errors: occur while the program is running if the environment
detects an operation that is impossible to carry out.
Logic Errors: occur when a program doesn't perform the way it was
intended
Syntax Errors: Arise because the rules of the language have not been
followed. They are detected by the compiler.
Department of
www.snsgroup.com
IT Information
Technology Drsnsrcas/CPreprocessor/April 27, 2024
Slide No : 4
Example
Class Error
{
public static void main(String args[])
{
int a=10;
int b=5;
int c=5;
int x=a/(b+c);
System.out.println("x=" +x);
int y=a/(b-c); // Errorr division by zero
System.out.println("y=" +y);
}
}
Department of
www.snsgroup.com
IT Information
Technology
Drsnsrcas/CPreprocessor/April 27, 2024 Slide No : 5
Errors and Error Handling
Some typical causes of errors:
Memory errors (i.e. memory incorrectly allocated, memory leaks,
“null pointer”)
File system errors (i.e. disk is full, disk has been removed)
Network errors (i.e. network is down, URL does not exist)
Calculation errors (i.e. divide by 0)
Department of
www.snsgroup.com
IT Information
Technology Drsnsrcas/CPreprocessor/April 27, 2024
Slide No : 7
• Errors and Error Handling
Department of
www.snsgroup.com
IT Information
Technology Drsnsrcas/CPreprocessor/April 27, 2024
Slide No :8
• Exceptions
Department of
www.snsgroup.com
IT Information
Technology Drsnsrcas/CPreprocessor/April 27, 2024
Slide No :9
• Coding Exceptions
• Coding Exceptions
• Try-Catch Mechanism
– Wherever your code may trigger an exception, the normal code logic is
placed inside a block of code starting with the “try” keyword:
– After the try block, the code to handle the exception should it arise is
placed in a block of code starting with the “catch” keyword.
Department of
www.snsgroup.com
IT Information
Technology Drsnsrcas/CPreprocessor/April 27, 2024
Slide No :10
Catching Exceptions
• Wrap code to be checked in a try-block
– checking occurs all the way down the execution
stack
• try-blocks can be nested
– control resumes at most enclosed matching
handler
Coding Exceptions
• Types of Exceptions
– Examples:
• public void myMethod throws Exception {
• public void myMethod throws IOException {
• try { … }
catch (Exception e) { … }
• try { … }
catch (IOException ioe) { … }
conclusion
– Exceptions are a powerful error handling
mechanism.
– Exceptions in Java are built into the language.
– Exceptions can be handled by the programmer
(try-catch), or handled by the Java environment
(throws).
– Exception handling can only hide the errors.
– It cannot correct the errors.
Department of
www.snsgroup.com
IT
Information
Technology Drsnsrcas/CPreprocessor/April 27, 2024