Java unit-3
Pdf👇👇👇
1b)what are the use of throw and throws clauses for exception handling?
Throw: throw keyword is used to throw an exception explicitly.
• It is useful for throwing an exception based on certain condition.
Syntax: throw new Exception_type("message");
Throws: Throws keyword is used along with function it might throw one of the listed exception.
• The caller to these method has to handle the exception using a try-catch.
Example
package q;
public class ThrowThrowsExample {
void A(int n) throws ArithmeticException, ClassNotFoundException {
if (n == 1) throw new ClassNotFoundException("Exception 1 ");
else throw new ArithmeticException("Exception 2");
}
public static void main(String[] args) {
ThrowThrowsExample obj = new ThrowThrowsExample();
try {
obj.A(1);
}
catch (Exception e) {
System.out.println(e);
}
}
Output: java.lang.ClassNotFoundException:Exception 1
2a) How to achieve synchronisation among thread?
Program:
class First {
synchronized public void display(String msg) {
System.out.print("["+msg);
try {
Thread.sleep(3000);
}
catch(InterruptedException e) {
System.out.println("error is"+e);
}
System.out.print("]");
}
}
class Second extends Thread {
String msg;
First fobj;
Second(First fp,String str)
{
fobj=fp;
msg=str;
start();
}
public void run() {
fobj.display(msg);
}
}
public class SynchroMethod {
public static void main(String args[])
{
First fnew=new First();
Second s1= new Second(fnew,"welcome");
Second s2= new Second(fnew,"new");
Second s3= new Second(fnew,"program");
}
}output: [welcome][program][new]
2b)Explain how to create own exception in Java with example.
package k;
import java.util.Scanner;
class MyException extends Exception {
MyException() {
System.out.println("Insufficient balance");
}
}
public class B {
public static void main(String[] args) {
int amount=10000,withdraw=0;
System.out.println("Enter amount to withdraw:");
Scanner sc=new Scanner(System.in);
withdraw=sc.nextInt();
try {
if(withdraw>amount)
throw new MyException();
else
System.out.println("remaining balance:"+(amount-withdraw));
}
catch(Exception e) {
System.out.println(e);
}
}
}Output: Enter amount to withdraw
11500
Insufficient Balance
K.MyException
3a) Wap that include try block and catch clause with arithmetic exception generated by zero.
package q;
public class ThrowThrowsExample {
public static void main(String[] args) {
int a = 50, b = 0, c;
try {
c = a / b;
System.out.println(a + "/" + b + "=" + c);
} catch (ArithmeticException e) {
System.out.println(e);
}
}Output: java.lang.ArithmeticException:divide by zero
3b) Explain checked and unchecked exception with example.
Checked
• It occurs at compile time
it is also called as compile time exception
we can handle this type of exception during compilation
The compiler will give an error if the code does not handle in checked exception
JVM need to catch and handle the checked exception
Checked exception mainly occurs when the chance of failure are too high
Example: File not found, IO exception,class not found exception.
Program:
import java.io.*;
public class checkedException {
public static void main(String[] args) {
try {
File obj1 = new File("abhi.txt");
FileReader obj2 = new FileReader(obj1);
System.out.println("Successful");
}
catch (Exception e) {
System.out.println(e);
}
}
}Output:java.io.FileNotFoundException:A.txt(No such file or directory)
Unchecked exception:
It occurs at run time
it is also called as run time exception
During compilation we cannot handle unchecked exception because they are generated at run time
Compiler will not give any error either exception are handled or not
JVM need not catch and handle the unchecked exception
it occurs due to the programming mistake
example: Array index out of bound exception, arithmetic exception, null pointer exception
Program:
public class uncheckedException {
public static void main(String[] args) {
int a = 50, b = 0, c;
c = a / b;
System.out.println("Ans: " + c);
}
}
Output: java.lang.ArithmeticException: divide by zero
0 Comments