SlideShare a Scribd company logo
3/18/2019 Exception Handling: Your Guided Course Template
https://canvas.instructure.com/courses/1480238/pages/exception-handling?module_item_id=21012886 1/3
Excep on Handling
Exception: An exception is an unwanted or unexpected event, which occurs during the execution of a
program i.e. at run time, which disrupts the normal flow of the program’s instructions.
Exception-in-java.png
Some of these exceptions are caused by user error, others by programmer error, and others by physical
resources that have failed in some manner. Based on these, we have three categories of Exceptions.
You need to understand them to know how exception handling works in Java.
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.
Java Exception Keywords:
There are 5 keywords which are used in handling exceptions in Java.
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.
3/18/2019 Exception Handling: Your Guided Course Template
https://canvas.instructure.com/courses/1480238/pages/exception-handling?module_item_id=21012886 2/3
Exception Handling:
Java exception handling is managed via five keywords: try, catch, throw, throws, and finally. Briefly, here
is how they work. Program statements that you think can raise exceptions are contained within a try
block. If an exception occurs within the try block, it is thrown. Your code can catch this exception (using
catch block) and handle it in some rational manner. System-generated exceptions are automatically
thrown by the Java run-time system. To manually throw an exception, use the keyword throw. Any
exception that is thrown out of a method must be specified as such by a throws clause. Any code that
absolutely must be executed after a try block completes is put in a finally block.
class Demo {
public static void main (String[] args) {
int[] arr = new int[4]; // array of size 4.
int i = arr[4]; // this statement causes an exception
System.out.println("Hi, I want to execute"); // the following statement will never
execute
}
}
Output :
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4
at Demo.main(Demo.java:9)
Explanation : In the above example an array is defined with size i.e. you can access elements only from
index 0 to 3. But you trying to access the elements at index 4(by mistake) that’s why it is throwing an
exception.In this case, JVM terminates the program abnormally. The statement System.out.println(“Hi, I
want to execute”); will never execute. To execute it, we must handled the exception using try-catch.
Hence to continue normal flow of the program, we need try-catch clause.
Note:
In a method, there can be more than one statements that might throw exception, So put all these
statements within its own try block and provide separate exception handler within own catch block for
each of them.
If an exception occurs within the try block, that exception is handled by the exception handler
associated with it. To associate exception handler, we must put catch block after it. There can be
more than one exception handlers. Each catch block is a exception handler that handles the
exception of the type indicated by its argument. The argument, ExceptionType declares the type of
the exception that it can handle and must be the name of the class that inherits from Throwable class.
For each try block there can be zero or more catch blocks, but only one finally block.
3/18/2019 Exception Handling: Your Guided Course Template
https://canvas.instructure.com/courses/1480238/pages/exception-handling?module_item_id=21012886 3/3
The finally block is optional.It always gets executed whether an exception occurred in try block or not
. If exception occurs, then it will be executed after try and catch blocks. And if exception does not
occur then it will be executed after the try block. The finally block in java is used to put important
codes such as clean up code e.g. closing the file or closing the connection.

More Related Content

ODP
Exception Handling In Java 15734
PPTX
Exception handling in java
PPTX
Exception handling in java
PPTX
Exception handling in JAVA
PPTX
Exception Handling in Java
PPTX
Exception handling in java
ODP
Exception Handling In Java
PPT
exception handling in java
Exception Handling In Java 15734
Exception handling in java
Exception handling in java
Exception handling in JAVA
Exception Handling in Java
Exception handling in java
Exception Handling In Java
exception handling in java

What's hot (20)

PPT
Exception handling
PPTX
Exception handling in java
PDF
summarizer16fev16_Exceptions
PPTX
Exception handling in Java
PPT
12 exception handling
PPT
Exception Handling Java
PPTX
Exception handling in java
PPTX
Z blue exception
PPT
Exceptionhandling
PDF
Exception handling
PPTX
Java Exception Handling
PDF
Best Practices in Exception Handling
PDF
Python exception handling
PDF
Java unit 11
PDF
Creating your own exception
DOCX
Exceptions handling notes in JAVA
PPTX
Exceptions overview
PPT
Chapter13 exception handling
PPT
Understanding Exception Handling in .Net
PPTX
Exception handling
Exception handling
Exception handling in java
summarizer16fev16_Exceptions
Exception handling in Java
12 exception handling
Exception Handling Java
Exception handling in java
Z blue exception
Exceptionhandling
Exception handling
Java Exception Handling
Best Practices in Exception Handling
Python exception handling
Java unit 11
Creating your own exception
Exceptions handling notes in JAVA
Exceptions overview
Chapter13 exception handling
Understanding Exception Handling in .Net
Exception handling
Ad

Similar to Exception handling basic (20)

PPTX
Exception Handling Multithreading: Fundamental of Exception; Exception types;...
PPTX
UNIT-3.pptx Exception Handling and Multithreading
PPTX
Exception Handling.pptx
PPTX
Module 4.pptxModule 4.pptxModuModule 4.pptxModule 4.pptxle 4.pptx
PPTX
Exception‐Handling in object oriented programming
PPTX
UNIT 2.pptx
PPTX
Java SE 11 Exception Handling
PDF
B.Sc. III(VI Sem) Advance Java Unit1: Exception Handling & Multithreading
PPTX
Interface andexceptions
PPT
8.Exception handling latest(MB).ppt .
PDF
Exception handling
PPT
Exception Handling in JAVA
PPTX
unit 4 msbte syallbus for sem 4 2024-2025
DOCX
Java Exception handling
PPTX
Exception handling in java
PPTX
7.error management and exception handling
PPTX
Exception Handling,finally,catch,throw,throws,try.pptx
PDF
JAVA PPT -4 BY ADI.pdf
DOCX
Class notes(week 8) on exception handling
PPTX
Unit 4 exceptions and threads
Exception Handling Multithreading: Fundamental of Exception; Exception types;...
UNIT-3.pptx Exception Handling and Multithreading
Exception Handling.pptx
Module 4.pptxModule 4.pptxModuModule 4.pptxModule 4.pptxle 4.pptx
Exception‐Handling in object oriented programming
UNIT 2.pptx
Java SE 11 Exception Handling
B.Sc. III(VI Sem) Advance Java Unit1: Exception Handling & Multithreading
Interface andexceptions
8.Exception handling latest(MB).ppt .
Exception handling
Exception Handling in JAVA
unit 4 msbte syallbus for sem 4 2024-2025
Java Exception handling
Exception handling in java
7.error management and exception handling
Exception Handling,finally,catch,throw,throws,try.pptx
JAVA PPT -4 BY ADI.pdf
Class notes(week 8) on exception handling
Unit 4 exceptions and threads
Ad

More from TharuniDiddekunta (16)

PDF
String class
PDF
Built in exceptions
PDF
Packages access protection, importing packages
PDF
Interfaces in java
PDF
Inheritance used in java
PDF
Operators, control statements represented in java
PDF
Classes, objects, methods, constructors, this keyword in java
PDF
Arrays in java
PPTX
Software Metrics (Testing)
PPTX
unit 3 Design 1
PPTX
Unit 4 testing
PPTX
risk managment and quality
PPTX
PPT
Network layer
PPTX
Transport layer and Application layer
PPT
Congection control and Internet working
String class
Built in exceptions
Packages access protection, importing packages
Interfaces in java
Inheritance used in java
Operators, control statements represented in java
Classes, objects, methods, constructors, this keyword in java
Arrays in java
Software Metrics (Testing)
unit 3 Design 1
Unit 4 testing
risk managment and quality
Network layer
Transport layer and Application layer
Congection control and Internet working

Recently uploaded (20)

PPTX
Unit 5 BSP.pptxytrrftyyydfyujfttyczcgvcd
PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PDF
Model Code of Practice - Construction Work - 21102022 .pdf
PPTX
additive manufacturing of ss316l using mig welding
PDF
Structs to JSON How Go Powers REST APIs.pdf
PPTX
OOP with Java - Java Introduction (Basics)
PDF
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
PPTX
Strings in CPP - Strings in C++ are sequences of characters used to store and...
PDF
Digital Logic Computer Design lecture notes
PDF
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
PPTX
Lecture Notes Electrical Wiring System Components
PPTX
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
PPTX
Geodesy 1.pptx...............................................
PPTX
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
DOCX
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
PPTX
CH1 Production IntroductoryConcepts.pptx
PPTX
Internet of Things (IOT) - A guide to understanding
PPTX
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
PPTX
Fluid Mechanics, Module 3: Basics of Fluid Mechanics
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
Unit 5 BSP.pptxytrrftyyydfyujfttyczcgvcd
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
Model Code of Practice - Construction Work - 21102022 .pdf
additive manufacturing of ss316l using mig welding
Structs to JSON How Go Powers REST APIs.pdf
OOP with Java - Java Introduction (Basics)
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
Strings in CPP - Strings in C++ are sequences of characters used to store and...
Digital Logic Computer Design lecture notes
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
Lecture Notes Electrical Wiring System Components
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
Geodesy 1.pptx...............................................
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
CH1 Production IntroductoryConcepts.pptx
Internet of Things (IOT) - A guide to understanding
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
Fluid Mechanics, Module 3: Basics of Fluid Mechanics
Embodied AI: Ushering in the Next Era of Intelligent Systems

Exception handling basic

  • 1. 3/18/2019 Exception Handling: Your Guided Course Template https://canvas.instructure.com/courses/1480238/pages/exception-handling?module_item_id=21012886 1/3 Excep on Handling Exception: An exception is an unwanted or unexpected event, which occurs during the execution of a program i.e. at run time, which disrupts the normal flow of the program’s instructions. Exception-in-java.png Some of these exceptions are caused by user error, others by programmer error, and others by physical resources that have failed in some manner. Based on these, we have three categories of Exceptions. You need to understand them to know how exception handling works in Java. 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. Java Exception Keywords: There are 5 keywords which are used in handling exceptions in Java. 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.
  • 2. 3/18/2019 Exception Handling: Your Guided Course Template https://canvas.instructure.com/courses/1480238/pages/exception-handling?module_item_id=21012886 2/3 Exception Handling: Java exception handling is managed via five keywords: try, catch, throw, throws, and finally. Briefly, here is how they work. Program statements that you think can raise exceptions are contained within a try block. If an exception occurs within the try block, it is thrown. Your code can catch this exception (using catch block) and handle it in some rational manner. System-generated exceptions are automatically thrown by the Java run-time system. To manually throw an exception, use the keyword throw. Any exception that is thrown out of a method must be specified as such by a throws clause. Any code that absolutely must be executed after a try block completes is put in a finally block. class Demo { public static void main (String[] args) { int[] arr = new int[4]; // array of size 4. int i = arr[4]; // this statement causes an exception System.out.println("Hi, I want to execute"); // the following statement will never execute } } Output : Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4 at Demo.main(Demo.java:9) Explanation : In the above example an array is defined with size i.e. you can access elements only from index 0 to 3. But you trying to access the elements at index 4(by mistake) that’s why it is throwing an exception.In this case, JVM terminates the program abnormally. The statement System.out.println(“Hi, I want to execute”); will never execute. To execute it, we must handled the exception using try-catch. Hence to continue normal flow of the program, we need try-catch clause. Note: In a method, there can be more than one statements that might throw exception, So put all these statements within its own try block and provide separate exception handler within own catch block for each of them. If an exception occurs within the try block, that exception is handled by the exception handler associated with it. To associate exception handler, we must put catch block after it. There can be more than one exception handlers. Each catch block is a exception handler that handles the exception of the type indicated by its argument. The argument, ExceptionType declares the type of the exception that it can handle and must be the name of the class that inherits from Throwable class. For each try block there can be zero or more catch blocks, but only one finally block.
  • 3. 3/18/2019 Exception Handling: Your Guided Course Template https://canvas.instructure.com/courses/1480238/pages/exception-handling?module_item_id=21012886 3/3 The finally block is optional.It always gets executed whether an exception occurred in try block or not . If exception occurs, then it will be executed after try and catch blocks. And if exception does not occur then it will be executed after the try block. The finally block in java is used to put important codes such as clean up code e.g. closing the file or closing the connection.