0% found this document useful (0 votes)
19 views

programs of exception handling

The document presents Java code examples demonstrating exception handling, specifically focusing on ArithmeticException and the use of try-catch-finally blocks. It illustrates how exceptions can be caught and handled, as well as the behavior of the finally block which always executes regardless of exceptions. Additionally, it shows a scenario where an exception is not handled by the catch block, highlighting the importance of matching exception types.

Uploaded by

ribhu
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)
19 views

programs of exception handling

The document presents Java code examples demonstrating exception handling, specifically focusing on ArithmeticException and the use of try-catch-finally blocks. It illustrates how exceptions can be caught and handled, as well as the behavior of the finally block which always executes regardless of exceptions. Additionally, it shows a scenario where an exception is not handled by the catch block, highlighting the importance of matching exception types.

Uploaded by

ribhu
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/ 3

//Arithmetic Exception

public class JavaExceptionExample


{
public static void main(String args[])
{
try
{
//code that may raise exception
int data=100/0;
}
catch(ArithmeticException e)
{System.out.println(e);
System.out.println(“Hello”);
}
//rest code of the program
System.out.println("rest of the code...");
}
}

Output:

Exception in thread main java.lang.ArithmeticException:/ by zero


rest of the code...

// Java program to demonstrate finally block in java When exception rise


and handled by catch
import java.io.*;
class GFG
{
public static void main(String[] args)
{
try {
System.out.println("inside try block");

// Throw an Arithmetic exception


System.out.println(34 / 0);
}

// catch an Arithmetic exception


catch (ArithmeticException e)
{

System.out.println("catch : exception handled.");


}

// Always execute
finally {

System.out.println("finally : i execute always.");


}
}
}

// Java program to demonstrate finally block When exception rise and not
handled by catch

import java.io.*;

class GFG {
public static void main(String[] args)
{
try {
System.out.println("Inside try block");

// Throw an Arithmetic exception


System.out.println(34 / 0);
}

// Can not accept Arithmetic type exception


// Only accept Null Pointer type Exception
catch (NullPointerException e) {
System.out.println(
"catch : exception not handled.");
}

// Always execute
finally {

System.out.println(
"finally : i will execute always.");
}
// This will not execute
System.out.println("i want to run");
}
}

You might also like