0% found this document useful (0 votes)
47 views2 pages

Java Exception Handling Basics

Exception handling in Java allows programmers to handle errors and unexpected events that occur during program execution. Exceptions disrupt the normal flow of a program. Java provides keywords like try, catch, and throw to handle exceptions. The try block contains code that might throw exceptions. The catch block handles any exceptions thrown in the try block. A try block must be followed by at least one catch or a finally block. An example program divides a number by zero, triggering an ArithmeticException that is caught and handled in the catch block.

Uploaded by

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

Java Exception Handling Basics

Exception handling in Java allows programmers to handle errors and unexpected events that occur during program execution. Exceptions disrupt the normal flow of a program. Java provides keywords like try, catch, and throw to handle exceptions. The try block contains code that might throw exceptions. The catch block handles any exceptions thrown in the try block. A try block must be followed by at least one catch or a finally block. An example program divides a number by zero, triggering an ArithmeticException that is caught and handled in the catch block.

Uploaded by

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

BS(SE)-17B

SEC : A
“EXCEPTION HANDLING”

DEFINITION:
“Exception is an error event that can happen during the execution of a
program and disrupts its normal flow”. Java provides specific keywords for
exception handling purposes, we will look after them first and then we will
write a simple program showing how to use them for exception handling.

1. Hit the Exception


TRY
2. Throw the Exception

3. Catch the Exception


CATCH
4. Handle the Exception

Try block:
The try block contains set of statements where an exception can occur. A
try block is always followed by a catch block, which handles the exception
that occurs in associated try block. A try block must be followed by catch
blocks or finally block or both.

Syntax of try block

5. try{
6. //statements that may cause an exception
7. }

Catch block:
A catch block is where you handle the exceptions, this block must follow
the try block. A single try block can have several catch blocks associated
with it. You can catch different exceptions in different catch blocks. When
an exception occurs in try block, the corresponding catch block that
handles that particular exception executes.

SYED DANIYAL SHAHID


1560-2017
SIR SAGHIR AHMED
BS(SE)-17B

SEC : A
Syntax of try catch in java

try
{
//statements that may cause an exception
}
catch (exception(type) e(object))
{
//error handling code
}

EXAMPLE:
class Example1 {
public static void main(String args[]) {
int num1, num2;
try {
/* We suspect that this block of statement can throw
* exception so we handled it by placing these statements
* inside try and handled the exception in catch block
*/
num1 = 0;
num2 = 62 / num1;
[Link](num2);
[Link]("Hey I'm at the end of try block");
}
catch (ArithmeticException e) {
/* This block will only execute if any Arithmetic exception
* occurs in try block
*/
[Link]("You should not divide a number by zero");
}
catch (Exception e) {
/* This is a generic Exception handler which means it can handle
* all the exceptions. This will execute if the exception is not
* handled by previous catch blocks.
*/
[Link]("Exception occurred");
}
[Link]("I'm out of try-catch block in Java.");
}
}

SYED DANIYAL SHAHID


1560-2017
SIR SAGHIR AHMED

You might also like