AKTU B.
Tech II-Year
Gateway Classes
Semester -IV CS IT & Allied Branches
BCS403-Object Oriented Programming with Java
UNIT-2 :Exception Handling
Gateway Series for Engineering
Topic Wise Entire Syllabus
Long - Short Questions Covered
AKTU PYQs Covered
DPP
Result Oriented Content
For Full Courses including Video Lectures
AKTU B. Tech II-Year
Gateway Classes
BCS403- Object Oriented Programming with Java
Unit-2
Introduction to Exception Handling
Syallbus
Exception Handling: The Idea behind Exception, Exceptions & Errors,
Types of Exception, Control Flow in Exceptions, JVM Reaction to
Exceptions, Use of try, catch, finally, throw, throws in Exception
Handling, In-built and User Defined Exceptions, Checked and Un-
Checked Exceptions.
Input /Output Basics: Byte Streams and Character Streams, Reading
and Writing File in Java
. Multithreading: Thread, Thread Life Cycle, Creating Threads, Thread
Priorities, Synchronizing Threads, Inter-thread Communication.
For Full Courses including Video Lectures
What We will Discuss Today?
• What is Exception Handling?
• Why its Required?
• Difference between Errors and Exceptions
• Try, catch, finally, throw and throws
• Library Defined Exceptions
• Custom Exceptions
• Difference b/w Checked and Unchecked Exceptions
Types of Errors in a Program
• Syntax Error
• Logical Error
• Runtime Errors
Runtime Error/Exceptions
Exception Hierarchy
1. import [Link];
2. class A
3. {
4. public static void main(String[] ar)
5. {
6. Scanner sc=new Scanner([Link]);
7. int[] arr={23,45,8,54,23,56};
8. int i,a,ans=0;
9. [Link]("Enter Index:");
10. try{
11. i=[Link]();
12. [Link]("Enter a Value:");
13. a=[Link]();
14. ans=arr[i]/a;
15. }catch(Exception ee)
16. {
17. [Link]("Some Error Occurred");
18. }
19. [Link]("Ans="+ans);
20. }
21. }
1. import [Link];
2. class A
3. {
4. public static void main(String[] ar)
5. {
6. Scanner sc=new Scanner([Link]);
7. int[] arr={23,45,8,54,23,56};
8. int i,a,ans=0;
9. [Link]("Enter Index:");
10. try{
11. i=[Link]();
12. [Link]("Enter a Value:");
13. a=[Link]();
14. ans=arr[i]/a;
15. }catch(ArrayIndexOutOfBoundsException ee)
16. {
17. [Link]("Array Index Problem");
18. }catch(ArithmeticException ee)
19. {
20. [Link]("Second Value Can't be zero");
21. }
22. [Link]("Ans="+ans);
23. }
24. }
1. import [Link];
2. class A
3. {
4. public static void main(String[] ar)
5. {
6. Scanner sc=new Scanner([Link]);
7. int[] arr={23,45,8,54,23,56};
8. int i,a,ans=0;
9. [Link]("Enter Index:");
10. try{
11. i=[Link]();
12. [Link]("Enter a Value:");
13. a=[Link]();
14. ans=arr[i]/a;
15. }catch(ArrayIndexOutOfBoundsException ee)
16. {
17. [Link]("Array Index Problem");
18. }catch(ArithmeticException ee)
19. {
20. [Link]("Second Value Can't be zero");
21. }finally
22. {
23. [Link]("Finally Block");
24. }
25. [Link]("Ans="+ans);
26. }
27. }
Exception Handling
import [Link].*;
class A41
{
public static void main(String[] ar)
{
Scanner sc=new Scanner([Link]);
int[] arr={23,45,67,78,34,12,47,78};
int i,a,ans=0;
[Link]("Enter Index:");
try{
i=[Link]();
[Link]("Enter Value:");
a=[Link]();
ans=arr[i]/a;
}catch(Exception ee)
{
[Link]("Some Error Occurred");
}
[Link]("Ans="+ans);
}
}
--------------------------------------------------------------------------------
import [Link].*;
class A42
{
public static void main(String[] ar)
{
Scanner sc=new Scanner([Link]);
int[] arr={23,45,67,78,34,12,47,78};
int i,a,ans=0;
[Link]("Enter Index:");
try{
i=[Link]();
[Link]("Enter Value:");
a=[Link]();
ans=arr[i]/a;
}catch(ArrayIndexOutOfBoundsException ee)
{
[Link]("Index should be <8");
}catch(InputMismatchException ee)
{
[Link]("Enter numbers Only");
}catch(Exception ee)
{
[Link]("Some error");
}
[Link]("Ans="+ans);
}
}
--------------------------------------------------------------------------------
import [Link].*;
class A43
{
public static void main(String[] ar)
{
Scanner sc=new Scanner([Link]);
int[] arr={23,45,67,78,34,12,47,78};
int i,a,ans=0;
[Link]("Enter Index:");
try{
i=[Link]();
[Link]("Enter Value:");
a=[Link]();
ans=arr[i]/a;
}catch(ArrayIndexOutOfBoundsException ee)
{
[Link]("Index should be <8");
}catch(InputMismatchException ee)
{
[Link]("Enter numbers Only");
}finally
{
[Link]("Finally Block");
}
[Link]("Ans="+ans);
}
}
--------------------------------------------------------------------------------
import [Link].*;
class InvalidAgeException extends RuntimeException
{
public String toString()
{
return "InvalidAgeException:Valid age should be >18 and <50";
}
}
class A44
{
public static void main(String[] ar)
{
Scanner sc=new Scanner([Link]);
int age;
[Link]("Enter Age:");
age=[Link]();
if(age<18 || age>50)
{
[Link]("Invalid Age");
InvalidAgeException ee=new InvalidAgeException();
throw ee;
}
[Link]("A1");
[Link]("A2");
[Link]("A3");
[Link]("A4");
}
}
--------------------------------------------------------------------------------
import [Link].*;
class InvalidAgeException extends Exception
{
public String toString()
{
return "InvalidAgeException:Valid age should be >18 and <50";
}
}
class A45
{
public static void main(String[] ar)
{
Scanner sc=new Scanner([Link]);
int age;
[Link]("Enter Age:");
age=[Link]();
if(age<18 || age>50)
{
[Link]("Invalid Age");
InvalidAgeException ee=new InvalidAgeException();
try{
throw ee;
}catch(Exception e)
{
}
}
[Link]("A1");
[Link]("A2");
[Link]("A3");
[Link]("A4");
}
}
--------------------------------------------------------------------------------
import [Link].*;
class InvalidAgeException extends Exception
{
public String toString()
{
return "InvalidAgeException:Valid age should be >18 and <50";
}
}
class A45
{
public static void main(String[] ar) throws InvalidAgeException
{
Scanner sc=new Scanner([Link]);
int age;
[Link]("Enter Age:");
age=[Link]();
if(age<18 || age>50)
{
[Link]("Invalid Age");
InvalidAgeException ee=new InvalidAgeException();
throw ee;
}
[Link]("A1");
[Link]("A2");
[Link]("A3");
[Link]("A4");
}
}
IO Package Programs
import [Link].*;
class A47
{
public static void main(String[] ar)
{
try{
FileInputStream fin=new FileInputStream("d:/[Link]");
int a1=[Link]();
int a2=[Link]();
int a3=[Link]();
[Link](5);
byte[] b=new byte[4];
[Link](b);
[Link](a1);
[Link](a2);
[Link](a3);
[Link](b[0]);
[Link](b[1]);
[Link](b[2]);
[Link](b[3]);
int a4=[Link]();
[Link]("Remaining:"+a4);
[Link]();
}catch(Exception ee)
{
[Link](ee);
}
}
}
--------------------------------------------------------------------
import [Link].*;
class A48
{
public static void main(String[] ar)
{
try{
FileInputStream fin=new FileInputStream("[Link]");
int a1=[Link]();
while(a1>=0)
{
[Link]((char)a1);
a1=[Link]();
}
[Link]();
}catch(Exception ee)
{
[Link](ee);
}
}
}
--------------------------------------------------------------------
import [Link].*;
class A49
{
public static void main(String[] ar)
{
try{
FileInputStream fin=new FileInputStream("[Link]");
int size=[Link]();
byte[] data=new byte[size];
[Link](data);
String str=new String(data);
[Link](str);
[Link]();
}catch(Exception ee)
{
[Link](ee);
}
}
}
--------------------------------------------------------------------
import [Link].*;
class A50
{
public static void main(String[] ar)
{
try{
FileOutputStream fout=new FileOutputStream("[Link]");
[Link](65);
[Link](66);
[Link](67);
byte[] b={97,98,99,100,101,102,103};
[Link](b);
[Link](b,2,3);
[Link]();
}catch(Exception ee)
{
[Link](ee);
}
}
}
--------------------------------------------------------------------
import [Link].*;
import [Link].*;
class A51
{
public static void main(String[] ar)
{
try{
Scanner sc=new Scanner([Link]);
[Link]("Source File:");
String src=[Link]();
[Link]("Destination File:");
String dstn=[Link]();
FileInputStream fin=new FileInputStream(src);
int size=[Link]();
byte[] data=new byte[size];
[Link](data);
[Link]();
FileOutputStream fout=new FileOutputStream(dstn);
[Link](data);
[Link]();
[Link]("Done");
}catch(Exception ee)
{
[Link](ee);
}
}
}
--------------------------------------------------------------------
import [Link].*;
import [Link].*;
class A52
{
public static void main(String[] ar)
{
try{
FileReader fr=new FileReader("[Link]");
BufferedReader bfr=new BufferedReader(fr);
String s1=[Link]();
String s2=[Link]();
String s3=[Link]();
[Link](s1);
[Link](s2);
[Link](s3);
[Link]();
}catch(Exception ee)
{
[Link](ee);
}
}
}
--------------------------------------------------------------------
import [Link].*;
import [Link].*;
class A53
{
public static void main(String[] ar)
{
try{
FileInputStream fr=new FileInputStream("[Link]");
InputStreamReader inr=new InputStreamReader(fr);
BufferedReader bfr=new BufferedReader(inr);
String s1=[Link]();
String s2=[Link]();
String s3=[Link]();
[Link](s1);
[Link](s2);
[Link](s3);
[Link]();
}catch(Exception ee)
{
[Link](ee);
}
}
}
--------------------------------------------------------------------
import [Link].*;
import [Link].*;
class A54
{
public static void main(String[] ar)
{
try{
InputStreamReader inr=new InputStreamReader([Link]);
BufferedReader bfr=new BufferedReader(inr);
String s1=[Link]();
String s2=[Link]();
String s3=[Link]();
[Link](s1);
[Link](s2);
[Link](s3);
}catch(Exception ee)
{
[Link](ee);
}
}
}
IO Exercise
Write a Java program to get a list of all file/directory names in the given directory.
(Hint: Use File Class)
Write a Java program to check if a file or directory specified by pathname exists or
not.
(Hint: Use File Class)
Write a Java program to read input from the Java console.
Write a Java program to read file content line by line.
Write a Java program to store text file content line by line in an array.
Write a Java program to read the first 3 lines of a file.
Write a Java program to find the longest word in a text file.
--------------------------------------------------------------------
Threads Programs
class Thread1 extends Thread
{
public void run()
{
for(int i=1;i<=10;i++)
{
[Link]("I="+i);
}
}
}
class Thread2 implements Runnable
{
public void run()
{
for(int k=11;k<=20;k++)
{
[Link]("K="+k);
}
}
}
class A55
{
public static void main(String[] ar)
{
Thread1 t1=new Thread1();
Thread t2=new Thread(new Thread2());
[Link]("Starting 1st");
[Link]();
[Link]("Starting 2nd");
[Link]();
[Link]("End of Main");
}
}
--------------------------------------------------------------------
class A56
{
public static void main(String[] ar)
{
for(int i=1;i<=10;i++)
{
[Link](i);
try{
[Link](1000);
}catch(Exception ee){}
}
}
}
--------------------------------------------------------------------
class Shared
{
int x=10;
}
class Thread1 extends Thread
{
Shared ss;
Thread1(Shared s)
{
ss=s;
}
public void run()
{
[Link]("A1");
[Link]("A2");
[Link]("A3");
// Critical Section Start
synchronized(ss)
{
[Link]("{");
[Link]("X="+ss.x);
ss.x=ss.x*2;
[Link](" X="+ss.x);
[Link]("}");
}
// Critical End
[Link]("A4");
[Link]("A5");
[Link]("A6");
}
}
class Thread2 extends Thread
{
Shared ss;
Thread2(Shared s)
{
ss=s;
}
public void run()
{
[Link]("B1");
[Link]("B2");
[Link]("B3");
// Critical Section Start
synchronized(ss)
{
[Link]("[");
[Link]("x="+ss.x);
ss.x=ss.x+2;
[Link](" x="+ss.x);
[Link]("]");
}
// Critical Section End
[Link]("B4");
[Link]("B5");
[Link]("B6");
}
}
class A57
{
public static void main(String[] ar)
{
Shared s=new Shared();
Thread1 t1=new Thread1(s);
Thread2 t2=new Thread2(s);
[Link]();
[Link]();
}
}
--------------------------------------------------------------------
class Customers
{
int amount=0;
synchronized void deposit(int amt)
{
[Link]("Initial:"+amount);
amount=amount+amt;
[Link]("After Deposit:"+amount);
notify();
}
synchronized void withdrawl(int amt)
{
try{
wait();
}catch(Exception ee){}
[Link]("Initial:"+amount);
amount=amount-amt;
[Link]("After Withdraw:"+amount);
}
}
class Thread1 extends Thread
{
Customers cc;
Thread1(Customers c)
{
cc=c;
}
public void run()
{
[Link](2000);
}
}
class Thread2 extends Thread
{
Customers cc;
Thread2(Customers c)
{
cc=c;
}
public void run()
{
[Link](5000);
}
}
class A58
{
public static void main(String[] ar)
{
Customers c=new Customers();
Thread1 t1=new Thread1(c);
Thread2 t2=new Thread2(c);
[Link]();
[Link]();
}
}
Thread Exercise
Write a Java program that sorts an array of integers using
multiple threads.
Write a Java program that calculates the sum of all prime
numbers up to a given limit using multiple threads.
Write a Java program that creates a bank account with
concurrent deposits and withdrawals using threads.
Write a Java program to demonstrate Semaphore usage for
thread synchronization
AKTU B. Tech II-Year
Gateway Classes
Full Courses Available in App
AKTU [Link] I- Year : All Branches
AKTU [Link] II- Year
Branches : 1. CS IT & Allied
2. EC & Allied
3. ME & Allied
4. EE & Allied
Downlod App Now
V. Lectures
Full Pdf Notes
Courses AKTU PYQs
DPP