0% found this document useful (0 votes)
12 views13 pages

Exception Handling

The document explains Java Exception Handling, which is a mechanism to manage runtime errors and maintain the normal flow of an application. It discusses the advantages of exception handling, common causes of exceptions, and the use of keywords like try, catch, finally, throw, and throws. Additionally, it provides examples demonstrating the importance of using try-catch blocks to handle exceptions effectively.

Uploaded by

Eshu Jain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views13 pages

Exception Handling

The document explains Java Exception Handling, which is a mechanism to manage runtime errors and maintain the normal flow of an application. It discusses the advantages of exception handling, common causes of exceptions, and the use of keywords like try, catch, finally, throw, and throws. Additionally, it provides examples demonstrating the importance of using try-catch blocks to handle exceptions effectively.

Uploaded by

Eshu Jain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

TOPICS

EXCEPTION HANDLING
EXCEPTION HANDLING
• Exception Handling in Java is one of the effective means to handle the runtime errors so
that the regular flow of the application can be preserved. Java Exception Handling is a
mechanism to handle runtime errors such as ClassNotFoundException, IOException,
SQLException, etc.
• Exception is an unwanted or unexpected event, which occurs during the execution of a
program, i.e. at run time, that disrupts the normal flow of the program’s instructions.
Advantage of Exception Handling
• The core advantage of exception handling is to maintain the normal flow of the application.
An exception normally disrupts the normal flow of the application; that is why we need to
handle exceptions.
• Examples :
• Try
• Catch(
• Major reasons why an exception Occurs
• Invalid user input
• Device failure
• Loss of network connection
• Physical limitations (out of disk memory)
• Code errors
• Opening an unavailable file
Let's consider a scenario:
Suppose there are 10
statements in a Java
• statement 1;
program and an exception
• statement 2;
occurs at statement 5; the
• statement 3; rest of the code will not be
• statement 4; executed, i.e., statements 6
• statement 5;//exception occurs to 10 will not be executed.
• statement 6; However, when we perform
• statement 7; exception handling, the rest
• statement 8; of the statements will be
• statement 9; executed. That is why we
• statement 10; use exception handling
in Java.
Java Exception Keywords
Java provides five keywords that are used to handle the
exception. The following table describes each.
Keyword Description
try The "try" keyword is used to specify a block
where we should place an exception code. It
means we can't use try block alone. The try
block must be followed by either catch or
finally.
catch The "catch" block is used to handle the
exception. It must be preceded by try block
which means we can't use catch block alone.
It can be followed by finally block later.
finally The "finally" block is used to execute the
necessary code of the program. It is executed
whether an exception is handled or not.
throw The "throw" keyword is used to throw an
exception.
throws The "throws" keyword is used to declare
exceptions. It specifies that there may occur
an exception in the method. It doesn't throw
an exception. It is always used with method
signature.
Java try-catch block

• Java try block is used to enclose the code that might throw an
exception. It must be used within the method.
• If an exception occurs at the particular statement in the try block, the
rest of the block code will not execute. So, it is recommended not to
keep the code in try block that will not throw an exception.
• Java try block must be followed by either catch or finally block.
Syntax of Java try-catch

try{
//code that may throw an exception
}catch(Exception_class_Name ref){}
Syntax of try-finally block

Java catch block is used to handle the Exception by declaring the type of
exception within the parameter. The declared exception must be the parent
class exception ( i.e., Exception) or the generated exception type. However,
the good approach is to declare the generated type of exception.
Problem without exception handling
Let's try to understand the problem if we don't use a try-catch block.
Example 1
public class TryCatchExample1 {

public static void main(String[] args) {


int data=50/0; //may throw exception
System.out.println("rest of the code");
}
}
Output:
Exception in thread "main" java.lang.ArithmeticException: / by zero
As displayed in the above example, the rest of the code is not executed (in such case, the rest of the code
statement is not printed).
There might be 100 lines of code after the exception. If the exception is not handled, all the code below the
exception won't be executed.
Solution by exception handling
Let's see the solution of the above problem by a java try-catch block.

Example 2
TryCatchExample2.java Output:

public class TryCatchExample2 { java.lang.ArithmeticException: / by zero


public static void main(String[] args) { rest of the code
try
{
int data=50/0; //may throw exception
}
//handling the exception
catch(ArithmeticException e)
{
System.out.println(e);
}
System.out.println("rest of the code");
}
}
Example
Let's see an example to print a custom message on exception.

TryCatchExample5.java

public class TryCatchExample5 {

public static void main(String[] args) { Output:


try
{ Can't divided by zero
int data=50/0; //may throw exception
}
// handling the exception
catch(Exception e)
{
// displaying the custom message
System.out.println("Can't divided by zero");
}
}

}
MORE EXAMPLES

This will generate an error, because myNumbers[10] does


not exist.
The output will be
public class Main { something like this:
public static void main(String[ ] args) {
int[] myNumbers = {1, 2, 3};
System.out.println(myNumbers[10]); Exception in thread "main"
} java.lang.ArrayIndexOutOfB
} oundsException: 10
at
Main.main(Main.java:4)
If an error occurs, we can use try...catch to catch the error and execute
some code to handle it:

Example
public class Main {
public static void main(String[ ] args) { The output will be:
try {
int[] myNumbers = {1, 2, 3}; Something went wrong.
System.out.println(myNumbers[10]);
} catch (Exception e) {
System.out.println("Something went wrong.");
}
}
}
Example-Throw an exception if age is below 18 (print
"Access denied"). If age is 18 or older, print "Access
granted":
public class Main {
static void checkAge(int age) {
if (age < 18) { The output will be:
throw new ArithmeticException("Access denied - You
must be at least 18 years old."); Exception in thread "main"
} java.lang.ArithmeticException
else { : Access denied - You must be
System.out.println("Access granted - You are old at least 18 years old.
enough!"); at
}} Main.checkAge(Main.java:4)
public static void main(String[] args) { at
checkAge(15); // Set age to 15 (which is below 18...) Main.main(Main.java:12)
}}

You might also like