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

Exception Handling in Java

Exception handling is an important feature in Java that allows programs to handle runtime errors gracefully. Exceptions interrupt normal program flow and can cause termination if not handled. The try-catch block is used to handle exceptions - code in the try block may throw exceptions, which are then caught and handled in corresponding catch blocks to prevent program termination and provide meaningful messages to users. Finally blocks also allow executing code regardless of whether an exception was thrown.

Uploaded by

Pratiksha
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)
84 views

Exception Handling in Java

Exception handling is an important feature in Java that allows programs to handle runtime errors gracefully. Exceptions interrupt normal program flow and can cause termination if not handled. The try-catch block is used to handle exceptions - code in the try block may throw exceptions, which are then caught and handled in corresponding catch blocks to prevent program termination and provide meaningful messages to users. Finally blocks also allow executing code regardless of whether an exception was thrown.

Uploaded by

Pratiksha
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
You are on page 1/ 3

Exception handling in java

Exception handling is one of the most important feature of java programming that
allows us to handle the runtime errors caused by exceptions.

What is an Exception?
An Exception is an unwanted event that interrupts the normal flow of the program.
When an exception occurs program execution gets terminated. In such cases we get
a system generated error message. The good thing about exceptions is that they can
be handled in Java. By handling the exceptions we can provide a meaningful
message to the user about the issue rather than a system generated message, which
may not be understandable to a user.

Exception Handling
If an exception occurs, which has not been handled by programmer then program
execution gets terminated and a system generated error message is shown to the
user. Exception handling ensures that the flow of the program doesn’t break when
an exception occurs. 

Try Catch in Java – Exception handling


We will see try-catch block which is used for exception handling.

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


try{

// statement that may cause an exception

}
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. For example if an arithmetic exception occurs in try block then the
statements enclosed in catch block for arithmetic exception executes.

Syntax of try catch in java


try{

// statement that may cause an exception

Catch (exception(type) e(objects))

//error handling code

// Java program to demonstrate working of try,

// catch and finally

class Division {

public static void main(String[] args)

int a = 10, b = 5, c = 5, result;

try {

result = a / (b - c);

System.out.println("result" + result);
}

catch (ArithmeticException e)

System.out.println("Exception caught:Division by zero");

finally {

System.out.println("I am in final block");

You might also like