KEMBAR78
Exception Handling | PDF | Computer Programming | Software Engineering
0% found this document useful (0 votes)
9 views13 pages

Exception Handling

The document provides an overview of exception handling in programming, explaining the difference between checked and unchecked exceptions. It details how to use try-catch and finally blocks to manage exceptions, as well as the keywords 'throw' and 'throws' for handling exceptions in method signatures. Additionally, it includes a summary of key concepts and keywords related to exception handling.

Uploaded by

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

Exception Handling

The document provides an overview of exception handling in programming, explaining the difference between checked and unchecked exceptions. It details how to use try-catch and finally blocks to manage exceptions, as well as the keywords 'throw' and 'throws' for handling exceptions in method signatures. Additionally, it includes a summary of key concepts and keywords related to exception handling.

Uploaded by

pmsaravanan28
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Selenium

WebDriver Training
Exception Handling
The Golden Circle

What

What is Exception?
Abnormal Condition to disturbs the flow of execution

Why Why do we need to handle it?


Provide meaningful feedback, to improve program reliability

How to handle the Exception?


How Using try- catch block
Exception vs Error
Types of Exception
❖ Checked Exception/Compile time Exception:

▪ The exceptions that are checked during the compile-time


▪ Checked exceptions are typically caused by external factors outside of the
program's control

Examples:
File I/O errors, network errors, or database errors.

❖ UnChecked Exception/Runtime Exception

▪ An exception that occurs during the execution of a progra


▪ Unchecked exceptions are typically caused by programming errors

Examples:
Java Exceptions like null pointer exceptions ,arithmetic exceptions ..
Selenium Exception like NoSuchElement, StaleElementReference …
Handling-Un Checked Exception

To handle the run time exception:

try-catch block
try-finally block
try block

➢ The try block is used to enclose the code that might throw
an exception.

➢ The try block must be followed by either catch or finally. It


means, we can't use try block alone.

Syntax:

try{ try{
logic logic
}catch (Exception e){} }finally{ }
Catch block
➢ The catch block is used to handle the exception caught by the try block and provides
information to user about the exception

➢ It specifies the type of exception to catch and the code to execute when that
exception is caught.

➢ Multiple catch blocks can be used to catch different types of exceptions, allowing for
more specific error handling.

Syntax:
try{ try{
logic logic
}catch (Exception e){} }catch(AritmeticException a) {
}catch(Exception e){}
Finally block

The finally block is used to specify code that should be executed regardless of whether an
exception is thrown or not.

Common use cases for the finally block include closing resources such as files or database
connections, or releasing locks on shared resources.

Syntax:

try{ try{
logic logic
}finally{} }catch (Exception e){
}finally{}
Handling Checked Exception

➢ These are exceptions that must be declared in the method signature or handled
using a try-catch block.

➢ It allows custom exceptions to be created and thrown when necessary.

➢ checked exception extends the Exception class.

➢ To handle these exceptions using keywords

-throw
-throws
throw - keyword

➢ The "throw" is a keyword that is used to explicitly throw an exception within


a method or block of code.

➢ Used to throw exceptions based on certain conditions within a code block


and for throwing custom exceptions.

Syntax :

throw new ExceptionType(“Information”);


Throws -keyword
➢ The ‘throws’ keyword is used to declare the exception that a method may throw
during execution of program.

➢ It provides information about the exceptions to the programmer as well as to the


caller of the method that throws the exceptions

➢ The keyword is declared at method signature level

Example:
public void add() throws InterruptedException{
Thread.sleep(2000);
System.out.println(“Learning throws keyword”);
}
Summary

Exception ->abnormal condition impact the execution flow

Checked and unchecked Exceptions

Keywords – try, catch, finally, throw, throws


Interview Prep

Keyword Description
try The "try" keyword is used to specify a block where we should place exception
code. The try block must be followed by either catch or finally. It means, we can't
use try block alone.
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 important 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 doesn't throw an exception.
It specifies that there may occur an exception in the method. It is always used with
method signature.

You might also like