Java Concepts
Java Concepts
System.out.println(10/0);
}}
Output:
class FilenotFound_Demo {
output
C:\>javac FilenotFound_Demo.java
FilenotFound_Demo.java:8: error: unreported exception
FileNotFoundException; must be caught or declared to be thrown
FileReader fr = new FileReader(file);
^
1 error
Ex:
class Unchecked_Demo {
Output
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException:
5
at Exceptions.Unchecked_Demo.main(Unchecked_Demo.java:8)
Example
class MyClass {
public static void main(String[ ] args) {
try {
int[] myNumbers = {1, 2, 3};
System.out.println(myNumbers[10]);
} catch (Exception e) {
System.out.println("Something went wrong.");
}
System.out.println("hello");
}}
For each try block there can be zero or more catch blocks, but only one
finally block.
Case 1
Let's see the java finally example where exception doesn't occur.
class TestFinallyBlock{
public static void main(String args[]){
try{
int data=25/5;
System.out.println(data);
}
catch(NullPointerException e){System.out.println(e);}
finally{System.out.println("finally block is always executed");}
System.out.println("rest of the code...");
}
}
output: 5
finally block is always executed
rest of the code..
Case 2
Let's see the java finally example where exception occurs and not
handled.
class Test{
public static void main(String args[]){
try{
int data=25/0;
System.out.println(data);
}
catch(NullPointerException e){System.out.println(e);}
finally{System.out.println("finally block is always executed");}
System.out.println("rest of the code...");
}
}
output: finally block is always executed
Exception in thread "main" java.lang.ArithmeticException: / by zero
at Test.main(9.java:4)
Case 3
Let's see the java finally example where exception occurs and
handled
class TestFinallyBlock2{
public static void main(String args[]){
try{
int data=25/0;
System.out.println(data); }
catch(ArithmeticException e){System.out.println(e);}
finally{System.out.println("finally block is always executed");}
System.out.println("rest of the code..."); } } }
output: java.lang.ArithmeticException: / by zero
finally block is always executed
rest of the code...
IMP POINTS:1. a single try block can have any number of catch
blocks.
2. A generic catch block can handle all the exceptions. Whether it is
ArrayIndexOutOfBoundsException or ArithmeticException or
NullPointerException or any other type of exception, this handles all of
them.
3. If no exception occurs in try block then the catch blocks are
completely ignored.
4. Corresponding catch blocks execute for that specific type of
exception:
catch(ArithmeticException e) is a catch block that can hanlde
ArithmeticException
catch(NullPointerException e) is a catch block that can handle
NullPointerException
Multiple catch blocks:
class Example2{
public static void main(String args[]){
try{
int a[]=new int[7];
a[4]=30/0;
// System.out.println(a[10]);
System.out.println("First print statement in try block");
}
catch(ArithmeticException e){
System.out.println("Warning: ArithmeticException");
}
catch(ArrayIndexOutOfBoundsException e){
System.out.println("Warning: ArrayIndexOutOfBoundsException");
}
catch(Exception e){
System.out.println("Warning: Some Other exception");
}
System.out.println("Out of try-catch block...");
}}
Output:
Warning:ArithmeticException
Out of try-catch block...
2.class Example{
public static void main(String args[]){
try{
int arr[]=new int[7];
arr[10]=10/5;
System.out.println("Last Statement of try block");
}
catch(ArithmeticException e){
System.out.println("You should not divide a number by zero");
}
catch(ArrayIndexOutOfBoundsException e){
System.out.println("Accessing array elements outside of the
limit");
}
catch(Exception e){
System.out.println("Some Other Exception");
}
System.out.println("Out of the try-catch block");
}
}
Output:
Accessing array elements outside of the limit
Out of the try-catch block
In this case, the second catch block got executed because the code
throws ArrayIndexOutOfBoundsException. We are trying to access the
11th element of array in above program but the array size is only 7.
throw keyword in java with example:
An exception is unwanted problem that disturbs normal execution and
there is a need to indicate that something goes wrong to jvm we need
to raise the exception explicitly
The Java throw keyword cerates an exception object and handover it to
jvm spcially in the case of userdefined exceptions because jvm doesnt
know anything about user defined exceptions.
Simply this keyword is used to handover our cerated exception object
to jvm manually to indicate something goes wrong.
We can throw either checked or uncheked exception in java by throw
keyword. The throw keyword is mainly used to throw custom exception.
Ex: class test
{ public static void main(String args[])
{ System.out.println(10/0);
}}
here main method is responsible for to create arirhmetic exception
object and handover to jvm. There is no code to handle that exception
by jvm so at runtime we will get exception like
Exception in thread "main" java.lang.ArithmeticException: / by zero
at test.main(9.java:3)
here main method is responsible to create the exception object and
handover to jvm. All this happends internally.
Now we will see how to create our own exception object explicitly and
handover to jvm manually.
and there is no code to handle the exception for jvm. So finally we will
get runtime exception as above
at test.main(9.java:3)
Case study1:
class test {
{ System.out.print(10/0);
System.out.print("hello"); }}
at test.main(9.java:7)
class test {
System.out.print("hello"); }}
System.out.print("hello");
^ 1 error
class test {
^ 1 error
here throw keyword can raise only exception and errors which are sub
classes of throwable. Test() is not subclass of throwable class so we
will get incompatible type error at compile time.
at test.main(9.java:5)