Thanks to visit codestin.com
Credit goes to www.scribd.com

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

Unit 10 Exception Handling

The document discusses exception handling in Java, explaining that exceptions disrupt program flow and must be managed to prevent abnormal termination. It categorizes exceptions into checked, unchecked, and errors, and introduces keywords such as try, catch, finally, throw, and throws for handling exceptions. Additionally, it provides syntax examples for using try-catch blocks, throwing exceptions, and declaring exceptions in methods.

Uploaded by

rinkubaria465
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)
2 views2 pages

Unit 10 Exception Handling

The document discusses exception handling in Java, explaining that exceptions disrupt program flow and must be managed to prevent abnormal termination. It categorizes exceptions into checked, unchecked, and errors, and introduces keywords such as try, catch, finally, throw, and throws for handling exceptions. Additionally, it provides syntax examples for using try-catch blocks, throwing exceptions, and declaring exceptions in methods.

Uploaded by

rinkubaria465
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/ 2

OBJECT ORIENTED CONCEPTS & PROGRAMMING–1 (CORE JAVA)

Unit 10: EXCEPTION HANDLING


❖ Exception:-
• An exception (or exceptional event) is a problem that arises during the execution of a
program. When an Exception occurs the normal flow of the program is disrupted and
the program/Application terminates abnormally, which is not recommended,
therefore, these exceptions are to be handled.
• An exception can occur for many different reasons. Following are some scenarios
where an exception occurs.
• A user has entered an invalid data.
• A file that needs to be opened cannot be found.
• A network connection has been lost in the middle of communications or the JVM has
run out of memory.
❖ Types of Exceptions :
1. Checked exceptions − A checked exception is an exception that is checked (notified)
by the compiler at compilation-time, these are also called as compile time
exceptions. These exceptions cannot simply be ignored, the programmer should take
care of (handle) these exceptions.
2. Unchecked exceptions − An unchecked exception is an exception that occurs at the
time of execution. These are also called as Runtime Exceptions. These include
programming bugs, such as logic errors or improper use of an API. Runtime
exceptions are ignored at the time of compilation.
3. Errors − These are not exceptions at all, but problems that arise beyond the control
of the user or the programmer. Errors are typically ignored in your code because you
can rarely do anything about an error. For example, if a stack overflow occurs, an
error will arise. They are also ignored at the time of compilation.
❖ 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.
❖ try-catch block
➢ try block:-
• Java try block is used to enclose the code that might throw an exception. It must be
used within the method.
• Syntax of Java try-catch
try{
//code that may throw an exception
}catch(Exception_class_Name ref){}
➢ catch 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.
❖ throw Exception
• The Java throw keyword is used to throw an exception explicitly.
• We specify the exception object which is to be thrown. The Exception has some
message with it that provides the error description. These exceptions may be related
to user inputs, server, etc.
• We can throw either checked or unchecked exceptions in Java by throw keyword. It
is mainly used to throw a custom exception.
• The syntax of the Java throw keyword is given below.
throw new exception_class("error message");
❖ throws keyword
• The Java throws keyword is used to declare an exception. It gives an information to
the programmer that there may occur an exception.
• So, it is better for the programmer to provide the exception handling code so that
the normal flow of the program can be maintained.
• Syntax of Java throws
return_type method_name() throws exception_class_name{
//method code
}
❖ finally block
• Java finally block is a block used to execute important code such as closing the
connection, etc.
• Java finally block is always executed whether an exception is handled or not.
Therefore, it contains all the necessary statements that need to be printed
regardless of the exception occurs or not.
• The finally block follows the try-catch block.

You might also like