0% found this document useful (0 votes)
1 views8 pages

Exception Handling Java .Bibhash Raut

The document provides examples of Java exception handling, demonstrating both the absence and presence of try-catch blocks. It covers built-in exceptions such as ArithmeticException, ArrayIndexOutOfBoundsException, StringIndexOutOfBoundsException, and IOException, along with user-defined exceptions. Each example includes code snippets and outputs to illustrate how exceptions are handled in Java.

Uploaded by

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

Exception Handling Java .Bibhash Raut

The document provides examples of Java exception handling, demonstrating both the absence and presence of try-catch blocks. It covers built-in exceptions such as ArithmeticException, ArrayIndexOutOfBoundsException, StringIndexOutOfBoundsException, and IOException, along with user-defined exceptions. Each example includes code snippets and outputs to illustrate how exceptions are handled in Java.

Uploaded by

The Venom
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Java Exceptions Handling

DATE: 2026/07/06

#[Link] catch and try

public class Exception1 {


public static void main(String[ ] args) {
[Link]("Bibhash Raut,25BTRCN231"); int[]
myNumbers = {1, 2, 3};
[Link](myNumbers[10]); // error!
}
}

OUTPUT
Bibhash Raut,25BTRCN231 ERROR!
Exception in thread "main" [Link]: Index 10 out of
bounds for length 3 at [Link]([Link])

=== Code Exited With Errors ===

#2 With catch and try

public class Exception2 {


public static void main(String[ ] args) {
[Link]("Bibhash Raut,25BTRCN231"); try {
int[] myNumbers = {1, 2, 3};
[Link](myNumbers[10]);
} catch (Exception e) {
[Link]("Something went wrong.");
}
}
}
OUTPUT
Bibhash Raut,25BTRCN231 Something went
wrong.

=== Code Execution Successful ===

Built-in Excep ons 1.

Arithme cExcep on

class ArithmeticException_Demo
{
public static void main(String args[])
{
[Link] ("Bibhash Raut,25BTRCN231"); try {
int a = 30, b = 0;
int c = a/b; // cannot divide by zero
[Link] ("Result = " + c);
}
catch(ArithmeticException e) {
[Link] ("Can't divide a number by 0");
}
}
}
OUTPUT
Bibhash Raut,25BTRCN231
Can't divide a number by 0

=== Code Execution Successful ===

2. ArrayIndexOutOfBoundsExcep on

class ArrayIndexOutOfBound_Demo
{
public static void main(String args[])
{
[Link] ("Bibhash Raut,25BTRCN231"); try{
int a[] = new int[5];
a[6] = 9; // accessing 7th element in an array of
// size 5
}
catch(ArrayIndexOutOfBoundsException e){
[Link] ("Array Index is Out Of Bounds");
}
}
}
OUTPUT
Bibhash Raut,25BTRCN231
Array Index is Out Of Bounds

=== Code Execution Successful ===


3. StringIndexOutOfBoundsExcep on

class StringIndexOutOfBound_Demo
{
public static void main(String args[])
{
[Link]("Bibhash Raut,25BTRCN231"); try {
String a = "Bibhash Raut"; // length is 22 char
c = [Link](24); // accessing 25th element
[Link](c);
}
catch(StringIndexOutOfBoundsException e) {
[Link]("StringIndexOutOfBoundsException");
}
}
}
OUTPUT
Bibhash Raut,25BTRCN231
StringIndexOutOfBoundsException
=== Code Execution Successful ===

4. IOExcep on

import [Link]; class


IOException_Demo {

public static void main(String[] args)


{
[Link]("Bibhash Raut,25BTRCN231");
// Create a new scanner with the specified String
// Object
Scanner scan = new Scanner("Bibhash ");

// Print the line


[Link]("" + [Link]());

// Check if there is an IO exception


[Link]("Exception Output: "
+ [Link]());

[Link]();
}
}
OUTPUT
Bibhash Raut,25BTRCN231
Bibhash
Exception Output: null
=== Code Execution Successful ===

#User-Defined Excep ons

class MyException extends Exception


{
//store account information private static int
accno[] = {1001, 1002, 1003, 1004};

private static String name[] =


{"Bibhash", "Raju", "Chunu", "Sumit", "Krishna"};

private static double bal[] =


{10000.00, 12000.00, 5600.0, 999.00, 1100.55};

// default constructor
MyException() { }

// parameterized constructor
MyException(String str) { super(str); }

// write main() public static void


main(String[] args)
{
[Link]("Bibhash Raut,25BTRCN231");
try
{
// display the heading for the table
[Link]("ACCNO" + "\t" + "CUSTOMER" +
"\t" + "BALANCE");

// display the actual account information


for (int i = 0; i < 5 ; i++)
{
[Link](accno[i] + "\t" + name[i] +
"\t" + bal[i]);

// display own exception if balance < 1000


if (bal[i] < 1000)
{
MyException me = new
MyException("Balance is less than 1000"); throw
me;
}
}
} //end of try

catch (MyException e) {
[Link]();
}
}
}
OUTPUT
Bibhash Raut,25BTRCN231
ACCNO CUSTOMER BALANCE
1001 Bibhash 10000.0
1002 Raju 12000.0
1003 Chunu 5600.0
1004 Sumit 999.0
MyException: Balance is less than 1000
at
[Link]([Link]) ERROR!
at
[Link]/[Link](DirectMethodHandleAcce
[Link]) at [Link]/[Link]([Link]) at
[Link]/[Link]([Link]) at
[Link]/[Link]([Link]) at
[Link]/[Link]([Link])
=== Code Execution Successful ===

You might also like