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

JavaSession 6

Uploaded by

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

JavaSession 6

Uploaded by

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

---------------------Exception Handling-----------

Runtime Exception :- Arithmatic, Nullpointer, IndexoutOfBound

Ot Excedption :- IO Exception, SQL Exception, class not found Exception

Checked Exception :- Classes that directly inherit throwable class Eg :


IOException, SQLException, Indexoutofbound,

Unchecked Exception :- Rt Exception, Arithmatic Exception Nullpointer,

Some Keywords that are used to handle the Exceptions

try :- try block is used in that part of code where Exception can be occured.

catch :- It catch the Exception and which Exception is occured and print the
messages.

throw :- it is used to throw the Exception to the catch block.

throws :- It is used to define which type of exception can be occured, It is


written besides
the methods.

finally :- It run automatically, It run always(Exceute always).

Syntax:-
try{
//code where Exception can be occured
}
catch(type of Exception)
{
//print mssg }

----------------------------1-------------------
import java.util.*;

import javax.swing.text.html.HTMLEditorKit.Parser;

public class First {


public static void main(String[] args)
{
// int var1=10, var2=0;
//
// System.out.println(var1/var2);

// int ar[] = new int[]{4,5,6,7};


// System.out.println(ar[5]);

// String str=null;
// System.out.println(str.length());
}
}
-------------------------2-----------------

import java.util.*;

import javax.swing.text.html.HTMLEditorKit.Parser;

public class First {


public static void main(String[] args)
{
int var1=10, var2=2;

try {
System.out.println(var1/var2); }

catch(ArithmeticException e)
{
System.out.println(e.getMessage());
}

finally
{
System.out.println("It Runs Always");
}

}
}

-----------------------------3----------------------------
import java.util.*;

import javax.swing.text.html.HTMLEditorKit.Parser;

public class First {


public static void main(String[] args)
{
int var1=10, var2=2;
int ar[] = new int[]{4,5,6,7};

try {
System.out.println(var1/var2);
System.out.println(ar[5]); }

catch(IndexOutOfBoundsException | ArithmeticException e)
{
System.out.println(e.getMessage());
}

finally
{
System.out.println("It Runs Always");
}

}
}

------------------------------4-------------------------

import java.io.IOException;
import java.util.*;

import javax.swing.text.html.HTMLEditorKit.Parser;

public class First {

void f1() throws IOException


{
throw new IOException("Device not found");
}

void f2() throws IOException


{
f1();
}

void calling()
{
try {
f2();
}
catch (Exception e) {
System.out.println("Handled");
}
}

public static void main(String[] args)


{
First ob = new First();
ob.calling();
System.out.println("Tata");

}
}

You might also like