KEMBAR78
Exception Handling in Java 2 | PDF | Programming Paradigms | Software Development
0% found this document useful (0 votes)
18 views9 pages

Exception Handling in Java 2

Exception handling

Uploaded by

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

Exception Handling in Java 2

Exception handling

Uploaded by

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

finally, block

 block used to execute important code/ "cleanup" such as closing the connection, closing file
etc., or lets you execute code after try...catch, regardless of the result
 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, handles or not.
 It follows the try-catch block.
 The important statements to be printed can be placed in the finally block.

Flowchart

Note: If you don't handle the exception, before terminating the program, JVM executes finally block
(if any).

Usage
1. When an exception does not occur
Here the Java program does not throw any exception finally block is executed after the try block.

class Demo
{
public static void main(String a[])
{
try
{
int data=250/5; // code do not throw any exception
System.out.println(data);
}
catch(NullPointerException e) // catch wont executed
{
System.out.println(e);
}
finally //executed regardless of exception occurred or not
{
System.out.println("finally block is always executed");
}
System.out.println("rest of exception code...");
}
}

2. When an exception occurs but is not handled by the catch block


Here, the code throws an exception however the catch block cannot handle it. Despite
this, the finally block is executed after the try block and then the program terminates abnormally.

public class TestFinallyBlock


{
public static void main(String args[])
{
try
{
System.out.println("Inside the try block");
int data=25/0; //throws divided by zero exception
System.out.println(data);
}
//cannot handle Arithmetic type exceptions and can only accept Null Pointer type exception
catch(NullPointerException e)
{
System.out.println(e);
}
finally //executes regardless of the exception
{
System.out.println("finally, block is always executed");
}
System.out.println("rest of the code...");
}
}
o/p-

finally, block is always executed


rest of the code...

3. When an exception occurs and is handled by the catch block: finally execute regardless of
the exception.
Rule: For each try block there can be zero or more catch blocks, but only one finally block.
Note: The finally block will not be executed if the program exits (either by calling System.exit()
or by causing a fatal error that causes the process to abort).

throw keyword
 used to explicitly throw a single exception or throw an exception explicitly.
 We can throw either checked or unchecked exceptions in Java by throw keyword. It is mainly
used to throw a custom exception.
 Used to explicitly throw an exception within a method or block of code.
 create and throw an exception instance inside a method or block.
 We can also define our own set of conditions and throw an exception explicitly using throw
keyword.
 For example, we can throw ArithmeticException if we divide a number by another number.
Here, we just need to set the condition and throw exception using throw keyword.
 Single Exception: Only one instance of an exception can be thrown at a time.
 When it used it needs to be caught or handled immediately
Syntax:
throw new Exception Type();
Syntax:
throw throwableObject;
A throwable object is an instance of class Throwable or subclass of the Throwable class.
or
throw Instance i.e.,
throw new Exception_class("error message");
throw new IOException("sorry device error");
Where the Instance must be of type Throwable or subclass of Throwable. For example,
Exception is the sub class of Throwable and the user-defined exceptions usually extend the
Exception class.

Example 1: throw: Unchecked Exception


here we have created a method named validate() that accepts an integer as a parameter. If
the age is less than 18, we are throwing the ArithmeticException otherwise print a message
welcome to vote.

class Test
{
public static void validateAge(int age)
{
if (age < 18)
{
// Explicitly throw an exception
throw new IllegalArgumentException("Age must be 18 or older.");
}
System.out.println("Age is valid");
}

public static void main(String[] args)


{
validateAge(16); // This will throw an IllegalArgumentException
}
}

Example2:
class Main
{
public static void divideByZero()
{
throw new ArithmeticException("Trying to divide by 0");
}
public static void main(String[] args)
{
divideByZero();
}
}

throw: checked exception


import java.io.*;
class Main
{
public static void findFile() throws IOException
{
throw new IOException("File not found");
}
public static void main(String[] args)
{
try
{
findFile();
System.out.println("Rest of code in try block");
}
catch (IOException e)
{
System.out.println(e.getMessage());
}
}
}
The findFile() method throws an IOException with the message we passed to its constructor.
Note that since it is a checked exception, we must specify it in the throws clause.
User-defined Exception
// class represents user-defined exception
class UserDefinedException extends Exception
{
public UserDefinedException(String str)
{
// Calling constructor of parent Exception
super(str);
}
}
public class TestThrow3
{
public static void main(String args[])
{
try
{
// throw an object of user defined exception
throw new UserDefinedException("This is user-defined exception");
}
catch (UserDefinedException ude)
{
System.out.println("Caught the exception");
// Print the message from MyException object
System.out.println(ude.getMessage());
}
}
}
super(str): passes the provided message to the constructor of the parent class Exception,
allowing the message to be stored and later retrieved using the getMessage() method.

Example:
class Test
{
public static void main (String[] ar)
{
try {
// Explicitly throw an exception
throw new ArithmeticException("Division by zero");
}
catch (ArithmeticException e)
{
System.out.println("Caught: " + e);
}
}
}
When to use
It is usually used when a condition occurs that makes it impossible or undesirable to continue the
method execution.
Scenarios:
 When a specific error condition occurs: If your method encounters a situation that it cannot
handle, such as invalid input or unexpected state, you can use throw to signal this.
 For custom exception handling: You can use throw to throw custom exceptions if predefined
exceptions don't suit your needs.
throws:
1. Used in the method declaration to declare the type of exceptions that might occur within
it. Or Declares exceptions explicitly a method might throw
2. Used in the method signature to declare that the method might throw certain
exceptions.
3. Only used for checked exceptions and tells the compiler that a method might throw one
or more checked exceptions, which the caller must handle.
4. Declares checked exceptions that must be handled or propagated.
5. Can declare multiple exceptions, separated by commas at a time.
6. When it is used, Leaves handling to the method caller

Syntax:
accessModifier returnType methodName() throws ExceptionType1, ExceptionType2 …
{
// code
}

public void method () throws ExceptionType;


public void method () throws IOException, SQLException
{ // method code that may throw exceptions }
Example:
import java.io.IOException;
class Test
{
// Declares that this method might throw an IOException
public void readFile() throws IOException
{
// Logic that may throw an IOException
throw new IOException("File not found");
}
public static void main(String[] ar)
{
Test obj = new Test();
try
{
obj.readFile();
}
catch (IOException e)
{
System.out.println("Caught: " + e);
}
}
}

When to use:
 When your method might throw checked exceptions, like IOException, SQLException, etc.
 when you want to delegate exception handling to the caller of the method, instead of
catching it within the method.
 must declare checked exceptions using throws in the method signature if the method has
code that might throw them and you don't want to handle them within the method.
 Only for checked exceptions, unchecked exceptions don't need to be declared

import java.io.FileNotFoundException;
class Test
{
// This method declares it may throw a FileNotFoundException
void readFile(String fileName) throws FileNotFoundException
{
if (fileName == null)
{
throw new FileNotFoundException("File not found: " + fileName);
} // Reading file logic
}
public static void main(String[] args)
{
Test obj = new Test();
try {
obj.readFile(null); // This will throw a FileNotFoundException
}
catch (FileNotFoundException e)
{
System.out.println("Caught: " + e.getMessage());
}
}
}
Here throw used to explicitly throw exception from methods or block where as throws used to
declared that might occurred.

Throwing multiple exceptions


import java.io.*;
class Main
{
public static void findFile() throws NullPointerException, IOException, InvalidClassException
{

// code that may produce NullPointerException


………

// code that may produce IOException


………

// code that may produce InvalidClassException


………
}

public static void main(String[] args)


{
try
{
findFile();
}
catch(IOException e1)
{
System.out.println(e1.getMessage());
}
catch(InvalidClassException e2)
{
System.out.println(e2.getMessage());
}
}
}
Difference between throw and throws
The throw and throws is the concept of exception handling where the throw keyword throw the
exception explicitly from a method or a block of code whereas the throws keyword is used in
signature of the method.

Basis of throw throws


Differences

Java throw keyword is used throw an Java throws keyword is used in the
Definition
exception explicitly in the code, inside method signature to declare an exception
which might be thrown by the function
the function or the block of code.
while the execution of the code.

Type of exception Using throw


Using throws keyword, we can declare
keyword, we can only propagate
both checked and unchecked exceptions.
Usage unchecked exception i.e., the checked
However, the throws keyword can be used
exception cannot be propagated using
to propagate checked exceptions only.
throw only.

The throw keyword is followed by an The throws keyword is followed by class


Syntax
instance of Exception to be thrown. names of Exceptions to be thrown.

Declaration throw is used within the method. throws is used with the method signature.

We can declare multiple exceptions using


We are allowed to throw only one
Internal throws keyword that can be thrown by the
exception at a time i.e. we cannot
implementation method. For example, main() throws
throw multiple exceptions.
IOException, SQLException.

Exception Propagation
An exception is first thrown from the top of the stack and if it is not caught, it drops down the call
stack to the previous method. If not caught there, the exception again drops down to the previous
method, and so on until they are caught or until they reach the very bottom of the call stack. This is
called exception propagation.
Note: By default Unchecked Exceptions are forwarded in calling chain (propagated).
Exception Propagation Example
class TestException
{
void m()
{
int data=50/0;
}
void n()
{
m();
}
void p()
{
try
{
n();
}
catch(Exception e)
{System.out.println("exception handled");}
}
public static void main(String args[])
{
TestException obj=new TestException();
obj.p();
System.out.println("normal flow...");
}
}
In the above example exception occurs in the m() method where it is not handled, so it is propagated
to the previous n() method where it is not handled, again it is propagated to the p() method where
exception is handled.

Advantages of Exception Handling in Java

1. Easy Identification of Program Code and Error-Handling Code


2. Provision to Complete Program Execution
3. To create custom exceptions making the code recovery and debugging easier.
3. Propagation of Errors
4. Meaningful Error Reporting
5. Identifying Error Types

Application areas of exception handling


o File Handling (I/O Operations)
o FileNotFoundException
o IOException
o Database Operations
o SQLException
o SQLTimeoutException
o Networking Operations
o SocketException
o UnknownHostException
o Multithreading
o InterruptedException
o IllegalThreadStateException
o User Input Validation
o NumberFormatException
o Memory Management
o OutOfMemoryError
o StackOverflowError
o Data Conversion and Parsing
o ParseException
o ClassCastException
o GUI Applications
o AWTException
o HeadlessException
o Security and Authentication
o AccessControlException
o AuthenticationException
o API Development
o ApiException

Qns: difference between final finally and finalize

You might also like