Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
26 views

Java Unit4 ExceptionHandling

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

Java Unit4 ExceptionHandling

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Managing Errors and Exceptions

Errors: we write code to build an application, the application may contain errors. We are
responsible for debugging the application. Debugging is art of finding and removing errors in
a software code.
Types Of errors:
1. Compile time errors
2. Runtime errors
Compile Time Errors: All syntax errors are compile-time errors which are due to the poor
understanding of the programming language. A compiler identifies these errors. Once we fix
such errors program will be compiled successfully.
Ex:
• Missing Semicolons
• Missing Brackets of Classes and methods
• Misspelling of identifiers and keywords
• Incompatible types
• Use of undeclared variables, Etc.
Runtime Errors: If an error occurred during execution time of a program then we call it as
Runtime Error .Though the program compiled successfully it may not execute properly due to
logical errors like division with zero, stack overflow etc.
Most common runtime errors are:
• Dividing integer by zero
• Accessing an element out of bound from array
• Attempting to use negative size of array
• Passing parameters not in valid range for a method
• Trying to illegally change the state of a thread
• Trying to use null references of objects.
Etc.

Exceptions: An exception is a runtime error. When an exception occurred JVM


create exception-object and checks for the exception handling code. If no supported
code found the runtime system hands over the exception object to default exception Handler.

If we want to execute the remaining code of program we will write try, catch blocks to
handle the exception. Once the exception handling is taken as part of the program remaining
code executes successfully.

https://gvsnarasimha.blogspot.com/ 1
Common Java exceptions are: Exceptions in Java can be classified as
1. Checked exceptions
2. Unchecked exceptions

1.Checked exceptions: The Checked exception occur during the compilation time of a
program.

Ex: IOException, SQL Exception, ClassNotFound Exception

2.Unchecked Exceptions: Unchecked exceptions are checked by JVM.


RuntimeException and its subclasses are unchecked exceptions
Ex: ArithmeticException, IndexOutOfBoundsException

https://gvsnarasimha.blogspot.com/ 2
Exception Handling: The purpose of exception handling is to detect and report an event which
disrupt the normal flow of execution.
Exception handling can be implemented as follows
1. Find the problem (Hit the Exception)
2. Inform that an error has occurred (Throw the Exception)
3. Receive the error information (Catch the Exception)
4. Take corrective actions (Handle the Exception)
The error handling code basically consists of two segments One to detect and to throw
exception and the other to catch the exceptions and take appropriate action.
Syntax: …………………
try {
statements
}
catch(Exception Type e)
{
statements
}
………………….
………………….

A try block is used to write the code which may lead to the exception in a program and throws
an exception . catch block which is defined by the keyword catch used to handle the exception.
We write catch block immediately after the try block. Catch block contain group of statements
which are used to display the information of exception that has occurred.

https://gvsnarasimha.blogspot.com/ 3
// Java Program that make use of Exception Handling
class Ex{
public static void main(String ar[]){
int a=10,b=0,c;
try{
c = a/b; //Arithmetic exception
}
catch(ArithmeticException e)
{
System.out.println("Division by zero is not possible ");
}
}
}
Output: Division by zero is not possible

Multiple Catch statements: it is possible to write multiple catch statements as part of


catch block. When an exception occurred java treats multiple catch statements as cases in
switch statement. The first statement whose parameter matches the exception will be executed
and the remaining statements will be skipped.

Syntax…
………………….
try{
statements
}
catch(Exception Type 1 e){
statements
}
catch(Exception Type 2 e){
statements
}

………………….

catch(Exception Type e){


statements
}
………………….
………………….

https://gvsnarasimha.blogspot.com/ 4
// Java Program to demonstrate use of multiple catch statements
class Ex2{
public static void main(String ar[])
{
int array[] ={10,5},b=5,c ;
try{
c = array[2] / b-array[1];
}
catch(ArithmeticException e)
{
System.out.println("Division by zero ");
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("ArrayIndexOutOfBoundsException");
}
finally{
System.out.println("Example on multiple catch statements");
}

}
}
Output:
ArrayIndexOutOfBoundsException
Example on multiple catch statements

Finally Block: If we write code in finally block the code is guaranteed to execute. The finally
block will execute whether or not an exception is thrown.We use finally block to write code to
free up resources which are allocated in the beginning (Ex: closing a file, closing database
connectivity etc).

Syntax: try{
try{ ……….
……….. ………..}
……….. catch(….. ){
} ………..
finally{ ……….. }
………..
……….. catch( ….. ){
} ………..
……….. }
..
finally{
……….
………..}

https://gvsnarasimha.blogspot.com/ 5

You might also like