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

Java

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

Java

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

02-06-2024

Objectives
• Introduction
• What exceptions are for
Exception Handling in Java • Catching & Throwing exceptions
• Exception Specifications
• Standard Java Exceptions
• Exceptions and Polymorphism
Explained By: Abhishek
• The finally clause
Lokesh
Sumit
• Resource Management
Karan • Uncaught Exceptions

Introduction Error
• Due to design errors or coding errors, our • An error may produce an incorrect output or
programs may fail in unexpected ways during may terminate the execution of the program
execution. An exception is a condition that is abruptly or even may cause the system to
caused by run time error in the program. The crash. So it is our responsibility to detect and
purpose of the exception handling mechanism manage the error properly.
is to provide a means to detect and report an
“ecxceptional circumstances” .
02-06-2024

Types of error Example of Run Time error


Class Error
• Runtime Errors: occur while the program is {
running if the environment detects an public static void main(String args[])
{
operation that is impossible to carry out. int a=10;
int b=5;
• Logic Errors: occur when a program doesn't int c=5;
perform the way it was intended int x=a/(b+c);
• Syntax Errors: Arise because the rules of the System.out.println("x=" +x);
int y=a/(b-c); // Errorr division by zero
language have not been followed. They are System.out.println("y=" +y);
}
detected by the compiler. }

Errors and Error Handling Exceptions


• Exceptions – a better error handling • How do you handle exceptions?
– Exceptions are a mechanism that provides the – To handle the exception, you write a “try-catch”
best of both worlds. block. To pass the exception “up the chain”, you
– Exceptions act similar to method return flags in declare a throws clause in your method or class
that any method may raise and exception should it declaration.
encounter an error. – If the method contains code that may cause a
– Exceptions act like global error methods in that checked exception, you MUST handle the
the exception mechanism is built into Java; exception OR pass the exception to the parent
exceptions are handled at many levels in a class (remember, every class has Object as the
program, locally and/or globally. ultimate parent)
02-06-2024

Coding Exceptions Standard Java Exceptions


• Coding Exceptions
Throwable
• 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: Exception Error

– 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. Runtime
IO Exception
Exception

Catching Exceptions Coding Exceptions


• Wrap code to be checked in a try-block • Example
– checking occurs all the way down the execution – try {
stack … normal program code
• try-blocks can be nested }
catch(Exception e) {
– control resumes at most enclosed matching
… exception handling code
handler
}
02-06-2024

Coding Exceptions Code Examples


• Types of Exceptions • 1. Demonstration of an unchecked exception
– Examples: (NullPointerException)
• public void myMethod throws Exception { • 2. Demonstration of checked exceptions:
• public void myMethod throws IOException {
– Passing a DivideByZeroException
• try { … }
catch (Exception e) { … } – Handling a DivideByZeroException
• try { … }
catch (IOException ioe) { … }

Example conclusion
class error2
{
public static void main(String arg[])
– Exceptions are a powerful error handling
{
int a=10;
mechanism.
int b=5;
int c=5; – Exceptions in Java are built into the language.
int x,y;

{
try
– Exceptions can be handled by the programmer
}
x=a/(b-c);
(try-catch), or handled by the Java environment
{
catch(ArithmeticException e)
(throws).
System.out.println(“Division by Zero”);
}
Y=a/(b-c);
– Exception handling can only hide the errors.
System.out.println(“y=“+y);
} – It cannot correct the errors.
}

You might also like