SlideShare a Scribd company logo
1 of 11
What is an exception in java? handle exception in java
Java - exception. An exception (or extraordinary event) is a problem that occurs during the
execution of a program. When an exception occurs, the normal flow of the program is
interrupted and the program / application terminates abnormally, which is not recommended,
therefore, these exceptions have to be controlled.
Types of exception to handle exception in java
There are Two type of exception
 Checked Exception
Checked exception occur at compile time or generated by compiler.
 Unchecked Exception
Unchecked exception occured at run time
Type of Exception
Checked and unchecked exception type
handle exception in java
 ArithmeticException
It is thrown when an exceptional condition has occurred in an arithmetic operation.
Class: Java.lang.ArithmeticException
This is the built-in-class present in the java.lang package. This exception occurs when the integer
is divided by zero.
Example:
Class example 1
{
public static void main (String [] args[])
{
try{
int num1 = 30, num2 = 0;
int output = num1 / num2;
System.out.println ("Result:" + output);
}
Catch (ArithmeticException) {
System.out.println ("You should not divide a number by zero");
}
}
}
Output
You should not divide a number by zero
Explanation: In the above example I divided an integer by a zero and because of this an
arithmetic exception was thrown.
 ArrayIndexOutOfBoundsException
It is thrown to indicate that an array has been accessed with an invalid index. The index is either
negative or equal to or greater than the size of the array.
Class: Java.lang.ArrayIndexOutOfBoundsException
This exception occurs when you try to access an array index that does not exist. For example, if
the array has only 5 elements and we are trying to display the 7th element it will throw an
exception.
Example:
Class exception demo 2
{
public static void main (String [] args[])
{
try{
int a [] = new int [10];
Array contains only 10 elements
[11] = 4;
}
Catch (ArrayIndexOutOfBoundsException e) {
System.out.println ("ArrayIndexOutofBounds");
}
}
}
Result
ArrayIndexOutofBounds
 ClassNotFoundException
This exception is raised when we try to access a class whose definition is not found
Example
Package com.journaldev.exception;
Public class DataTest {
public static void main (String [] args) {
try {
Class.forName ("com.journaldev.MyInvisibleClass");
ClassLoader.getSystemClassLoader () loadClass ("com.journaldev.MyInvisibleClass").;
ClassLoader.getPlatformClassLoader () loadClass ("com.journaldev.MyInvisibleClass").;
} Catch (ClassNotFoundException e) {
e.printStackTrace ();
}
}
}
Result
ClassNotFoundExcepion
 FileNotFoundException
This exception is raised when a file is unreachable or does not open.
Example
Import java.io.BufferedReader;
Import java.io.File;
Import java.io.FileReader;
Import java.io.IOException;
Public class FileNotFoundExceptionExample {
Private static final string filename = "input.txt";
public static void main (String [] args) {
Bufferreader rd = null;
try {
// Open the file for reading.
rd = new BufferedReader (new FileReader (new File (filename)));
// Read all the contents of the file.
String inputline = null;
While ((inputline = rd.readLine ())! = Null)
Println (inputLine);
}
Catch (before IOException) {
System.err.println ("An IOException was caught!");
ex.printStackTrace ();
}
After all
// Close the file.
try {
rd.close ();
}
Catch (before IOException) {
System.err.println ("An IOException was caught!");
ex.printStackTrace ();
}
}
}
}
OutPut
FileNotFoundException
 IOException
It is thrown when the input-output operation fails or is interrupted
Example
Import java.util. *;
Public class ScannerIOExceptionExample1 {
public static void main (String [] args) {
// create a new scanner with the specified string object
ScannerScan = new Scanner ("Hello World! Hello My Programming School");
// print the line
System.out.println ("" + scan.nextLine ());
// check if there is an IO exception
System.out.println ("Exception output:" + scan.ioException ());
scan.close ();
} }
OutPut:
Hello World! Hello My Programming School Exception output: null
 InterruptedException
It is thrown when a thread is waiting, sleeping, or processing something, and it is interrupted.
Example
Package com.javaguides.corejava;
Class childhread's extension thread {
Public void run () {
try {
Thread.Sleep (1000);
} Catch (interrupted exception) {
System.err.println ("InterruptedException caught!");
e.printStackTrace ();
}
}
}
Public class InterruptedExceptionExample {
Public static void main (String [] args) throws InterruptedException {
ChildThread childThread = new ChildThread ();
childThread.start ();
childThread.interrupt ();
}
}
Out Put:
IntrruptedException
 NoSuchFieldException
It is thrown when a class does not contain a specified field (or variable)
 NoSuchMethodException
It is thrown when using a method that is not found.
 NullPointerException
This exception is raised when referring to members of a null object. Null represents nothing
Class: Java.lang.NullPointer Exception
Whenever a member is called with an "null" object, an object of this class is created.
Example
Class exception 2
{
public static void main (String [] args[])
{
try{
String str = null;
System.out.println (str.length ());
}
Catch (NullPointerException e) {
Println ("NullPointerException ..");
}
}
}
Result
NullPointerException ..
Here, there is a length () function, which should be used on an object. However the string object
str in the above example is null so it is not the object that caused the NullPointerException.
 NumberFormatException
This exception is raised when a method cannot convert a string to a numeric format.
Class: Java.lang.NumberFormatException
This exception occurs when a string is parsed on any numeric variable.
For example, the statement int num = Integer.parseInt ("XYZ"); The numbers will throw a
FormatException because the string "XYZ" cannot be parsed to int.
Example
Class ExceptionDemo3
{
public static void main (String [] args[])
{
try{
int num = Integer.parseInt ("XYZ");
Println (number);
} Catch (numberFormatException e) {
System.out.println ("Number format exception occurred");
}
}
}
Result
Number format exception occurred
 Runtime Exception
This represents any exception that occurs during runtime.
Example
You can see above example of FileNotFoundException
 StringIndexOutOfBoundsException
It is thrown by string class methods to indicate that the index is either negative by the size of the
string
Class: Java.lang.StringIndexOutOfBoundsException
Whenever an index is not implemented in a string, an object of this class is created, which is not
in the range.
Each character of a string object is stored in a special index starting at 0.
To get a character present in a particular index of a string we can use charAt (int), a method of
java.lang.tring. Where the difference is the logic index.
Example
Class ExceptionDemo4
{
public static void main (String [] args[])
{
try{
String str = "beginbook";
Println (str.length ()) ;;
char c = str.charAt (0);
c = str.charAt (40);
Println (c);
} Catch (StringIndexOutOfBoundsException e) {
Println ("StringIndexOutOfBoundsException !!");
}
}
}
Result
StringIndexOutOfBoundsException !!
For more Click HERE
Recommended Posts:
 Find the area of triangle and rectangle in java
 How to compare dates in java|algorithm with source code
 Java roll dice 10000 times with algorithm and source code
 Write a Java program that displays the number of characters, lines and words in a text
 Write a Java program that reads a file and displays the file on the screen with a line
number before each line
 Write a Java program that reads a file name from the user, then displays information
about whether the file exists, readable, writable, type of file and the length of the file in
bytes
 Java program to make frequency count of vowels, consonants, special symbols, digits,
words in a given text
 Write a Java program for sorting a given list of names in ascending order
 Write a java program to Checks whether a given string is a palindrome or not
 Write a java program to perform multiplication of two matrices
 write a java program that print the fibonacci series for a give number.
 Write a Java Program that find the factorial of a number
 Write a Java program that find prime numbers between 1 to n
 Write a Java program that prints all real and imaginary solutions to the quadratic equation
 Odd and Even number in java | Algorithm
 Even number in java | algorithm with source code
 find unique elements in array java | algorithm and source code
 Write a Java program for sorting a given list of names in ascending order
 write a java program that print the fibonacci series for a give number.
 Write a Java program that prints all real and imaginary solutions to the quadratic equation
 Missing Number in java with example
 Java greater number using loop
 Java Print function with example | Difference between print () and println () in java
 What is the Fibonacci series? in java | Displaying the Fibonacci sequence using a loop
 Odd and Even number in java | Algorithm
 Even number in java | algorithm with source code
 Java Area of Rectangle
 What is a polygon? | Area of Triangle in java
 To calculate Area of Circle in java program
 Java Addition through user input
 Producer consumer problem using threads in java
 Types of exception in java with examples
 Java applet program that displays a simple message
The exception occurred because the referenced index was not present in the string.

More Related Content

What's hot (13)

Chap12
Chap12Chap12
Chap12
 
Java - Exception Handling Concepts
Java - Exception Handling ConceptsJava - Exception Handling Concepts
Java - Exception Handling Concepts
 
Java exception handling
Java exception handlingJava exception handling
Java exception handling
 
Exception Handling in Java
Exception Handling in JavaException Handling in Java
Exception Handling in Java
 
Java -Exception handlingunit-iv
Java -Exception handlingunit-ivJava -Exception handlingunit-iv
Java -Exception handlingunit-iv
 
9781439035665 ppt ch11
9781439035665 ppt ch119781439035665 ppt ch11
9781439035665 ppt ch11
 
Chap2 exception handling
Chap2 exception handlingChap2 exception handling
Chap2 exception handling
 
Built in exceptions
Built in exceptions Built in exceptions
Built in exceptions
 
Exception Handling in Java
Exception Handling in JavaException Handling in Java
Exception Handling in Java
 
Exception
ExceptionException
Exception
 
Control statements
Control statementsControl statements
Control statements
 
Java exception handling
Java exception handlingJava exception handling
Java exception handling
 
Exeption handling
Exeption handlingExeption handling
Exeption handling
 

Similar to What is an exception in java?

Similar to What is an exception in java? (20)

exception handling
exception handlingexception handling
exception handling
 
UNIT 2.pptx
UNIT 2.pptxUNIT 2.pptx
UNIT 2.pptx
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in java
 
Exceptions
ExceptionsExceptions
Exceptions
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in java
 
Java unit 11
Java unit 11Java unit 11
Java unit 11
 
JAVA PROGRAMMING- Exception handling - Multithreading
JAVA PROGRAMMING- Exception handling - MultithreadingJAVA PROGRAMMING- Exception handling - Multithreading
JAVA PROGRAMMING- Exception handling - Multithreading
 
Java API, Exceptions and IO
Java API, Exceptions and IOJava API, Exceptions and IO
Java API, Exceptions and IO
 
Exceptions & Its Handling
Exceptions & Its HandlingExceptions & Its Handling
Exceptions & Its Handling
 
Exception handling in java.pptx
Exception handling in java.pptxException handling in java.pptx
Exception handling in java.pptx
 
Java-Exception Handling Presentation. 2024
Java-Exception Handling Presentation. 2024Java-Exception Handling Presentation. 2024
Java-Exception Handling Presentation. 2024
 
Exception Handling In Java Presentation. 2024
Exception Handling In Java Presentation. 2024Exception Handling In Java Presentation. 2024
Exception Handling In Java Presentation. 2024
 
Exception handling
Exception handlingException handling
Exception handling
 
Java exception handling
Java exception handlingJava exception handling
Java exception handling
 
java exception.pptx
java exception.pptxjava exception.pptx
java exception.pptx
 
Exception handling
Exception handlingException handling
Exception handling
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in java
 
Exception Handling
Exception HandlingException Handling
Exception Handling
 
Exception Handling
Exception HandlingException Handling
Exception Handling
 
Exception Handling
Exception HandlingException Handling
Exception Handling
 

Recently uploaded

Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...srsj9000
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Dr.Costas Sachpazis
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...Soham Mondal
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxupamatechverse
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Dr.Costas Sachpazis
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxpurnimasatapathy1234
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130Suhani Kapoor
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxupamatechverse
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024hassan khalil
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝soniya singh
 
Analog to Digital and Digital to Analog Converter
Analog to Digital and Digital to Analog ConverterAnalog to Digital and Digital to Analog Converter
Analog to Digital and Digital to Analog ConverterAbhinavSharma374939
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxwendy cai
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSSIVASHANKAR N
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVRajaP95
 

Recently uploaded (20)

Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptx
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
 
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptx
 
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptxExploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptx
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
 
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
 
Analog to Digital and Digital to Analog Converter
Analog to Digital and Digital to Analog ConverterAnalog to Digital and Digital to Analog Converter
Analog to Digital and Digital to Analog Converter
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptx
 
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCRCall Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
 

What is an exception in java?

  • 1. What is an exception in java? handle exception in java Java - exception. An exception (or extraordinary event) is a problem that occurs during the execution of a program. When an exception occurs, the normal flow of the program is interrupted and the program / application terminates abnormally, which is not recommended, therefore, these exceptions have to be controlled. Types of exception to handle exception in java There are Two type of exception  Checked Exception Checked exception occur at compile time or generated by compiler.  Unchecked Exception Unchecked exception occured at run time Type of Exception Checked and unchecked exception type
  • 2. handle exception in java  ArithmeticException It is thrown when an exceptional condition has occurred in an arithmetic operation. Class: Java.lang.ArithmeticException This is the built-in-class present in the java.lang package. This exception occurs when the integer is divided by zero. Example: Class example 1 { public static void main (String [] args[]) { try{ int num1 = 30, num2 = 0; int output = num1 / num2; System.out.println ("Result:" + output); } Catch (ArithmeticException) { System.out.println ("You should not divide a number by zero"); } } }
  • 3. Output You should not divide a number by zero Explanation: In the above example I divided an integer by a zero and because of this an arithmetic exception was thrown.  ArrayIndexOutOfBoundsException It is thrown to indicate that an array has been accessed with an invalid index. The index is either negative or equal to or greater than the size of the array. Class: Java.lang.ArrayIndexOutOfBoundsException This exception occurs when you try to access an array index that does not exist. For example, if the array has only 5 elements and we are trying to display the 7th element it will throw an exception. Example: Class exception demo 2 { public static void main (String [] args[]) { try{ int a [] = new int [10]; Array contains only 10 elements [11] = 4; } Catch (ArrayIndexOutOfBoundsException e) { System.out.println ("ArrayIndexOutofBounds"); } } } Result ArrayIndexOutofBounds  ClassNotFoundException This exception is raised when we try to access a class whose definition is not found Example
  • 4. Package com.journaldev.exception; Public class DataTest { public static void main (String [] args) { try { Class.forName ("com.journaldev.MyInvisibleClass"); ClassLoader.getSystemClassLoader () loadClass ("com.journaldev.MyInvisibleClass").; ClassLoader.getPlatformClassLoader () loadClass ("com.journaldev.MyInvisibleClass").; } Catch (ClassNotFoundException e) { e.printStackTrace (); } } } Result ClassNotFoundExcepion  FileNotFoundException This exception is raised when a file is unreachable or does not open. Example Import java.io.BufferedReader; Import java.io.File; Import java.io.FileReader;
  • 5. Import java.io.IOException; Public class FileNotFoundExceptionExample { Private static final string filename = "input.txt"; public static void main (String [] args) { Bufferreader rd = null; try { // Open the file for reading. rd = new BufferedReader (new FileReader (new File (filename))); // Read all the contents of the file. String inputline = null; While ((inputline = rd.readLine ())! = Null) Println (inputLine); } Catch (before IOException) { System.err.println ("An IOException was caught!"); ex.printStackTrace (); } After all // Close the file. try { rd.close (); } Catch (before IOException) { System.err.println ("An IOException was caught!"); ex.printStackTrace (); } } } } OutPut
  • 6. FileNotFoundException  IOException It is thrown when the input-output operation fails or is interrupted Example Import java.util. *; Public class ScannerIOExceptionExample1 { public static void main (String [] args) { // create a new scanner with the specified string object ScannerScan = new Scanner ("Hello World! Hello My Programming School"); // print the line System.out.println ("" + scan.nextLine ()); // check if there is an IO exception System.out.println ("Exception output:" + scan.ioException ()); scan.close (); } } OutPut: Hello World! Hello My Programming School Exception output: null  InterruptedException It is thrown when a thread is waiting, sleeping, or processing something, and it is interrupted.
  • 7. Example Package com.javaguides.corejava; Class childhread's extension thread { Public void run () { try { Thread.Sleep (1000); } Catch (interrupted exception) { System.err.println ("InterruptedException caught!"); e.printStackTrace (); } } } Public class InterruptedExceptionExample { Public static void main (String [] args) throws InterruptedException { ChildThread childThread = new ChildThread (); childThread.start (); childThread.interrupt (); } } Out Put: IntrruptedException  NoSuchFieldException It is thrown when a class does not contain a specified field (or variable)
  • 8.  NoSuchMethodException It is thrown when using a method that is not found.  NullPointerException This exception is raised when referring to members of a null object. Null represents nothing Class: Java.lang.NullPointer Exception Whenever a member is called with an "null" object, an object of this class is created. Example Class exception 2 { public static void main (String [] args[]) { try{ String str = null; System.out.println (str.length ()); } Catch (NullPointerException e) { Println ("NullPointerException .."); } } } Result NullPointerException .. Here, there is a length () function, which should be used on an object. However the string object str in the above example is null so it is not the object that caused the NullPointerException.  NumberFormatException This exception is raised when a method cannot convert a string to a numeric format. Class: Java.lang.NumberFormatException This exception occurs when a string is parsed on any numeric variable. For example, the statement int num = Integer.parseInt ("XYZ"); The numbers will throw a FormatException because the string "XYZ" cannot be parsed to int. Example
  • 9. Class ExceptionDemo3 { public static void main (String [] args[]) { try{ int num = Integer.parseInt ("XYZ"); Println (number); } Catch (numberFormatException e) { System.out.println ("Number format exception occurred"); } } } Result Number format exception occurred  Runtime Exception This represents any exception that occurs during runtime. Example You can see above example of FileNotFoundException  StringIndexOutOfBoundsException It is thrown by string class methods to indicate that the index is either negative by the size of the string Class: Java.lang.StringIndexOutOfBoundsException Whenever an index is not implemented in a string, an object of this class is created, which is not in the range. Each character of a string object is stored in a special index starting at 0. To get a character present in a particular index of a string we can use charAt (int), a method of java.lang.tring. Where the difference is the logic index. Example Class ExceptionDemo4 { public static void main (String [] args[]) { try{ String str = "beginbook";
  • 10. Println (str.length ()) ;; char c = str.charAt (0); c = str.charAt (40); Println (c); } Catch (StringIndexOutOfBoundsException e) { Println ("StringIndexOutOfBoundsException !!"); } } } Result StringIndexOutOfBoundsException !! For more Click HERE Recommended Posts:  Find the area of triangle and rectangle in java  How to compare dates in java|algorithm with source code  Java roll dice 10000 times with algorithm and source code  Write a Java program that displays the number of characters, lines and words in a text  Write a Java program that reads a file and displays the file on the screen with a line number before each line  Write a Java program that reads a file name from the user, then displays information about whether the file exists, readable, writable, type of file and the length of the file in bytes  Java program to make frequency count of vowels, consonants, special symbols, digits, words in a given text  Write a Java program for sorting a given list of names in ascending order  Write a java program to Checks whether a given string is a palindrome or not  Write a java program to perform multiplication of two matrices  write a java program that print the fibonacci series for a give number.  Write a Java Program that find the factorial of a number  Write a Java program that find prime numbers between 1 to n  Write a Java program that prints all real and imaginary solutions to the quadratic equation  Odd and Even number in java | Algorithm  Even number in java | algorithm with source code  find unique elements in array java | algorithm and source code  Write a Java program for sorting a given list of names in ascending order  write a java program that print the fibonacci series for a give number.  Write a Java program that prints all real and imaginary solutions to the quadratic equation  Missing Number in java with example  Java greater number using loop  Java Print function with example | Difference between print () and println () in java  What is the Fibonacci series? in java | Displaying the Fibonacci sequence using a loop
  • 11.  Odd and Even number in java | Algorithm  Even number in java | algorithm with source code  Java Area of Rectangle  What is a polygon? | Area of Triangle in java  To calculate Area of Circle in java program  Java Addition through user input  Producer consumer problem using threads in java  Types of exception in java with examples  Java applet program that displays a simple message The exception occurred because the referenced index was not present in the string.