This is set of program in java, You can also refer our previous post
This Articles Contents
Write a program to show exception in java (Arithmetic)
package com.onurdesk.exception.programs;
public class ExceptionsExample1 {
/**
* @author Onurdesk
*
* Thanks for visiting https://onurdesk.com
* like us on facebook :
* https://facebook.com/onurdesk
*/
public static void main(String[] args) {
try {
System.out.println("No exception there");
System.out.println(10 / 0);
System.out.println("Again and again No exception there");
} catch (Exception e) {
e.printStackTrace();
System.out.println(e.getMessage());
System.out.println(e.toString());
} finally {
System.out.println("Onurdesk");
System.out.println("Thanks for reading onurdesk");
}
System.out.println("Main Method ");
System.out.println("Thanks for visiting https://onurdesk.com");
}
}
Write a program to show exception in java (Arithmetic divide by zero)
package com.onurdesk.exception.programs;
public class ExceptionExample2 {
/**
* @author Onurdesk
*
* Thanks for visiting https://onurdesk.com like us on facebook :
* https://facebook.com/onurdesk
*/
public static void main(String[] args) {
try {
System.out.println("No exception there");
System.out.println(10 / 0);
System.out.println("Again and again No exception there");
} catch (NullPointerException e) {
e.printStackTrace();
System.out.println(e.getMessage());
System.out.println(e.toString());
} finally {
System.out.println("onurdesk");
System.out.println("Thanks for reading onurdesk");
}
System.out.println("Main Method ");
System.out.println("Thanks for visiting https://onurdesk.com");
}
}
Write a program to show exception in java (Null pointer)
package com.onurdesk.exception.programs;
public class ExceptionExample3 {
/**
* @author Onurdesk
*
* Thanks for visiting https://onurdesk.com like us on facebook :
* https://facebook.com/onurdesk
*/
public static void main(String[] args) {
try {
System.out.println("No exception there");
System.out.println("Again no exception There");
System.out.println("Again and again No exception there");
} catch (NullPointerException e) {
System.out.println(10 / 0);
} finally {
System.out.println(10 / 0);
System.out.println("Thanks for reading onurdesk");
}
System.out.println("Main Method ");
System.out.println("Thanks for visiting https://onurdesk.com");
}
}
Write a program to show exception in java (Arithmetic)
package com.onurdesk.exception.programs;
public class ExceptionExample4 {
/**
* @author Onurdesk
*
* Thanks for visiting https://onurdesk.com like us on facebook :
* https://facebook.com/onurdesk
*/
public static void main(String[] args) {
try {
System.out.println("No exception there");
System.out.println("Again no exception There");
System.out.println("Again and again No exception there");
} catch (Exception e) {
System.out.println("exception occurs");
} finally {
System.out.println(10 / 0);
}
System.out.println("Main Method ");
System.out.println("Thanks for visiting https://onurdesk.com");
}
}
Write a program to show exception in java (show the finally block)
package com.onurdesk.exception.programs;
public class ExceptionExample5 {
/**
* @author Onurdesk
*
* Thanks for visiting https://onurdesk.com
* like us on facebook :
* https://facebook.com/onurdesk
*/
public static void main(String[] args) {
try {
System.out.println("No exception there");
System.out.println("Again no exception There");
System.out.println("Again and again No exception there");
} catch (Exception e) {
System.out.println("Exeption Occurs");
} finally {
System.out.println("I am From Finally Block");
System.out.println("Thanks for reading onurdesk");
}
System.out.println(10 / 0);
System.out.println("Thanks for visiting https://onurdesk.com");
}
}
Write a program to show exception in java (Arithmetic)
package com.onurdesk.exception.programs;
public class ExceptionExample6 {
/**
* @author Onurdesk
*
* Thanks for visiting https://onurdesk.com like us on facebook :
* https://facebook.com/onurdesk
*/
public static void main(String[] args) {
doWork();
}
public static void doWork() {
doMoreWork();
System.out.println(10 / 0);
}
public static void doMoreWork() {
System.out.println("hello");
System.out.println("Thanks for visiting https://onurdesk.com");
}
}
Write a program to show exception in java (handelling exception)
package com.onurdesk.exception.programs;
public class ExceptionsExample7 {
/**
* @author Onurdesk
*
* Thanks for visiting https://onurdesk.com like us on facebook :
* https://facebook.com/onurdesk
*/
public static void main(String[] args) {
try {
System.out.println("No exception there");
System.out.println("Again No exception there");
System.out.println("Again and again No exception there");
} catch (Exception e) {
e.printStackTrace();
} finally {
System.out.println("Onurdesk");
System.out.println("Thanks for visiting https://onurdesk.com");
}
System.out.println("Main Method ");
}
}