00:00
/* 01 - Sop 1 9, with arithmatic error between. */
public class L02 {
public static void main(String[] args) {
System.out.println(1);
System.out.println(2);
System.out.println(3);
System.out.println(4);
System.out.println(100/0);
System.out.println(5);
System.out.println(6);
System.out.println(7);
System.out.println(8);
System.out.println(9);
}
}
/* 02 - c/T eX for FileInputStream w/o tR-cH. */
import java.io.FileInputStream;
public class L02 {
public static void main(String[] args) {
FileInputStream fis = new FileInputStream("d:/abc.text");
}
}
/* 03 - FileInputStream + tR-cH. */
//Checked exceptions - jisko complier checked kar sakta hai compile time par */
import java.io.FileInputStream;
public class L02 {
public static void main(String[] args) {
String filePath = System.getProperty("user.home") + "/Desktop/x/abc.java";
try{
FileInputStream fis = new FileInputStream(filePath);
System.out.println("file found");
}
catch(Exception e){
System.out.println(e);
}
}
}
// ab compiler isko complie kar deyga
// aur uske baad phir run time par file avability check kareyga ..
/* 04 - rT eX null, arith. */
public class L02 {
public static void main(String[] args) {
int a=100, b=0, c;
c=a/b;
System.out.println(c);
String name=null;
System.out.println(name.length()); // unchecked
}
}
/* 05 - tR-cH working with arth, eX. */
public class L02 {
public static void main(String[] args) {
try{
int a=100, b=0, c;
c=a/b;
System.out.println(c);
}
catch(Exception e){
System.out.println(e);
System.out.println("error in the code, pls check");
}
}
}
/* 06 - tR-cH with FileInputStream and Rest Code : file found, this is the proper flow code. */
import java.io.FileInputStream;
public class L02 {
public static void main(String[] args) {
String filePath = System.getProperty("user.home") + "/Desktop/x/abc.java";
try{
FileInputStream fis = new FileInputStream(filePath);
System.out.println("file found");
}
catch(Exception e){
System.out.println(e);
}
System.out.println("this is the proper flow code");
}
}
/* 07 - Arth. eX w/o tR-cH and Rest code : abnormal termination, java.lang.ArithmeticException. */
public class L02 {
public static void main(String[] args) {
int a=100, b=0, c;
c=a/b;
System.out.println(c);
System.out.println("normal flow");
}
}
/* 08 - Arth. rT eX with tR-cH and Rest code */
public class L02 {
public static void main(String[] args) {
try{
int a=100, b=0, c;
c=a/b;
System.out.println(c);
}
catch(Exception e){
System.out.println(e);
System.out.println("error in the code, pls check");
}
System.out.println("normal flow");// execute
}
}
/* 09 - Arth. rT eX with tR-cH and specified eX name */
public class L02 {
public static void main(String[] args) {
try{
int a=100, b=0, c;
c=a/b;
System.out.println(c);
}
catch(ArithmeticException e){
System.out.println(e);
System.out.println("error in the code, pls check");
}
System.out.println("normal flow");
}
}
/* 10 - Arth. rT eX w/o error with tR-cH and rest code */
public class L02 {
public static void main(String[] args) {
System.out.println("1");
try{
System.out.println("2");
int a=100, b=2, c;
System.out.println("3");
c=a/b;
System.out.println("4");
System.out.println(c);
System.out.println("5");
}
catch(ArithmeticException e){ // skiped block
System.out.println("6");
System.out.println(e);
System.out.println("7");
}
System.out.println("normal flow");
}
}
/* 11 - Arth. rT eX having error with tR-cH and rest code */
public class L02 {
public static void main(String[] args) {
System.out.println("1");
try{
System.out.println("2");
int a=100, b=0, c;
System.out.println("3");
c=a/b;
System.out.println("4");
System.out.println(c);
System.out.println("5");
}catch(ArithmeticException e){
System.out.println("6");
System.out.println(e);
System.out.println("7");
}
System.out.println("normal flow");
}
}
/* 12 - eX print type */
public class L02 {
public static void main(String[] args) {
try{
int a=100, b=0, c;
c=a/b;
System.out.println(c);
}catch(ArithmeticException e){
/* whole description */
e.printStackTrace();
/* blow both same - exception name and description */
System.out.println(e);
System.out.println(e.toString());
/* only message */
System.out.println(e.getMessage());
}
}
}
/* 13 - Arth. rT eX with tR-cH and fn/y w/o error */
public class L02 {
public static void main(String[] args) {
try{
int a=100, b=2, c;
c=a/b;
System.out.println(c);
}catch(ArithmeticException e){
System.out.println(e);
}finally{
System.out.println("Finally Block Executed, with no exception code ");
}
}
}
/* 14 - Arth. rT eX with tR-cH with fn/y having error */
public class L02 {
public static void main(String[] args) {
try{
int a=100, b=0, c;
c=a/b;
System.out.println(c);
}catch(ArithmeticException e){
System.out.println("the exception is = "+e);
}
finally{
System.out.println("Finally Block Executed, with no exception code ");
}
}
}
/* 15 - Arth. rT eX with tR with fn/y with error */
public class L02 {
public static void main(String[] args) {
try{
int a=100, b=0, c;
c=a/b;
System.out.println(c);
}finally{
System.out.println("Finally Block Executed, with out catch block ");
}
}
}
/* 16 - Arth. rT eX with tR-cH fn/l with error and rest code */
public class L02 {
public static void main(String[] args) {
try{
int a=100, b=0, c;
c=a/b;
System.out.println(c);
}finally{
System.out.println("Finally Block Executed, but other code body not ");
}
System.out.println("another code body");
}
}
/* 17 - tR block only */
public class L02 {
public static void main(String[] args) {
try{
// only try block
}
}
}
/* 18 - cH block only */
public class L02 {
public static void main(String[] args) {
catch(Exception e){
// error: 'catch' without 'try
}
}
}
/* 19 - fn/y block only */
public class L02 {
public static void main(String[] args) {
finally{
// error: 'finally' without 'try'
}
}
}
/* 20 - 1 tR multiple cH, same eX type */
public class L02 {
public static void main(String[] args) {
try{
}catch(Exception e){
}catch(Exception e){
// error: exception Exception has already been caught
}
}
}
/* 21 - 1 tR multiple cH, parent-child eX */
public class L02 {
public static void main(String[] args) {
try{
}catch(Exception e){
}catch(ArithmeticException e){
// exception ArithmeticException has already been caught
}
}
}
/* 22 - 1 tR multiple cH, chid-parent eX */
public class L02 {
public static void main(String[] args) {
try{
}catch(ArithmeticException e){
}catch(Exception e){
// works fine
}
}
}
/* 23 - try - finally block */
public class L02 {
public static void main(String[] args) {
try{
}finally{
System.out.println("hello"); // print hello
}
}
}
/* 24 - tR fn/y and cH block sequence */
public class L02 {
public static void main(String[] args) {
try{
}finally{
}catch(ArithmeticException e){
// error: 'catch' without 'try'
}
}
}
/* 25 - tR fn/y tR-cH sequence : works fine */
public class L02 {
public static void main(String[] args) {
try{
}finally{
}try{
}catch(ArithmeticException e){
}
}
}
/* 26 - tR nested tR-cH and cH. : works fine */
public class L02 {
public static void main(String[] args) {
try{
try{}
catch(Exception e){}
}catch(ArithmeticException e){
}
}
}
/* 27 - tR and cH with nested tR-cH */
public class L02 {
public static void main(String[] args) {
try{
}catch(ArithmeticException e){
try{}
catch(Exception ea){}
}
}
}
/* 28 - tR-cH with fn/y nested tR-cH block. : works fine */
public class L02 {
public static void main(String[] args) {
try{
}catch(ArithmeticException e){
}finally{
try{}
catch(Exception ea){}
}
}
}
/* 29 - tR block, code statement then cH block. */
public class L02 {
public static void main(String[] args) {
try{
}
System.out.println("ji"); //create error
catch(ArithmeticException e){
}finally{
try{}
catch(Exception ea){}
}
}
}
/* 30 - eX handle in calling m/T with tR-cH. : hello */
public class L02 {
public static void main(String[] args) {
L02 t1 = new L02();
t1.divide();
System.out.println("hello");
}
/* calling mt */
void divide(){
try{
int a=100, b=0, c;
c=a/b;
System.out.println(c);
}catch(ArithmeticException e){
e.printStackTrace();
}
}
}
/* 31 - eX handle in caller m/T with tR-cH and rest code. */
public class L02 {
public static void main(String[] args) {
L02 t1 = new L02();
try{
t1.divide();
}catch(ArithmeticException e){
e.printStackTrace();
}
System.out.println("hello");
}
void divide(){
int a=100, b=0, c;
c=a/b;
System.out.println(c);
}
}
/* 32 - c/T (i/o) e/X w/o throws : FileNotFoundException; */
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
public class L02 {
public static void main(String[] args) {
}
class ReadAndWrite{
void readFile() throws FileNotFoundException{
String filePath = System.getProperty("user.home") + "/Desktop/x/abc.java";
FileInputStream fis = new FileInputStream(filePath);
}
void saveFile(){
String filePath1 = System.getProperty("user.home") + "/Desktop/x/abc.java";
String text ="this is done";
FileOutputStream fos = new FileOutputStream(filePath1);
}
}
/* 33 - c/T e/X with throws */
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
public class L02 {
public static void main(String[] args) {
}
}
class ReadAndWrite{
void readFile() throws FileNotFoundException{
String filePath = System.getProperty("user.home") + "/Desktop/x/abc.java";
FileInputStream fis = new FileInputStream(filePath);
}
void saveFile()throws FileNotFoundException{
String filePath1 = System.getProperty("user.home") + "/Desktop/x/abc.java";
String text ="this is done";
FileOutputStream fos = new FileOutputStream(filePath1);
}
}
/* 34 - c/T e/X with throws but not handle in caller m/T : exception FileNotFoundException; */
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
public class L02 {
public static void main(String[] args) {
ReadAndWrite re = new ReadAndWrite();
re.readFile();
}
}
class ReadAndWrite{
void readFile() throws FileNotFoundException{
String filePath = System.getProperty("user.home") + "/Desktop/x/abc.java";
FileInputStream fis = new FileInputStream(filePath);
}
void saveFile()throws FileNotFoundException{
String filePath1 = System.getProperty("user.home") + "/Desktop/x/abc.java";
String text ="this is done";
FileOutputStream fos = new FileOutputStream(filePath1);
}
}
/* 35 - c/T e/X with throws and caller m/T handle it with t/R-c/H */
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
public class L02 {
public static void main(String[] args) {
ReadAndWrite re = new ReadAndWrite();
try{
re.readFile();
}
catch(FileNotFoundException e){
e.printStackTrace();
}
System.out.println("hello");
}
}
class ReadAndWrite{
void readFile() throws FileNotFoundException{
String filePath = System.getProperty("user.home") + "/Desktop/x/abc.java";
FileInputStream fis = new FileInputStream(filePath);
}
void saveFile()throws FileNotFoundException{
String filePath1 = System.getProperty("user.home") + "/Desktop/x/abc.java";
String text ="this is done";
FileOutputStream fos = new FileOutputStream(filePath1);
}
}
/* 36 - c/T e/X with throws to caller main m/T and main also throws with rest program */
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
public class L02 {
public static void main(String[] args) throws FileNotFoundException{
ReadAndWrite re = new ReadAndWrite();
re.readFile();
System.out.println("hello");
}
}
class ReadAndWrite{
void readFile() throws FileNotFoundException{
String filePath = System.getProperty("user.home") + "/Desktop/x/abc.java";
FileInputStream fis = new FileInputStream(filePath);
}
void saveFile()throws FileNotFoundException{
String filePath1 = System.getProperty("user.home") + "/Desktop/x/abc.java";
String text ="this is done";
FileOutputStream fos = new FileOutputStream(filePath1);
}
}
/* 37 - custom e/X with error ( checked e/X) */
class UnderAgeExceptions extends Exception{
UnderAgeExceptions(){
super();
}
UnderAgeExceptions(String message){
super(message);
}
}
public class L02 {
public static void main(String[] args) {
int age = 17;
if(age<18){
throw new UnderAgeExceptions(); // for no argument c/T
}
}
}
/* 38 - custom checked e/X w/o handle it */
class UnderAgeExceptions extends Exception{
UnderAgeExceptions(){
super();
}
UnderAgeExceptions(String message){
super(message);
}
}
public class L02 {
public static void main(String[] args) throws UnderAgeExceptions{
int age = 17;
if(age<18){
throw new UnderAgeExceptions(); // for no argument c/T
}
}
}
/* 39 - checked c/T e/X with try-catch */
class UnderAgeExceptions extends Exception{
UnderAgeExceptions(){
super();
}
UnderAgeExceptions(String message){
super(message);
}
}
public class L02 {
public static void main(String[] args) {
int age = 17;
try{
if(age<18){
throw new UnderAgeExceptions("EXCEPTION **: "); // parameterized
}
}catch( UnderAgeExceptions e){
e.printStackTrace();
}
System.out.println("hiii");
}
}
// complie, run but print defaut exception handler message
/* 40 - unchecked r/T e/X with throws */
class UnderAgeExceptions extends RuntimeException{
UnderAgeExceptions(){
super();
}
UnderAgeExceptions(String message){
super(message);
}
}public class L02 {
public static void main(String[] args) throws UnderAgeExceptions{
int age = 17;
if(age<18){
throw new UnderAgeExceptions("EXCEPTION RT **: "); // parameterized
}
System.out.println("hiii");
}
}
// complie, magar DEH ne abnormal terminate kara ahi prograom ko
/* 41 - unchecked r/T e/X with try-catch */
class UnderAgeExceptions extends RuntimeException{
UnderAgeExceptions(){
super();
}
UnderAgeExceptions(String message){
super(message);
}
}
public class L02 {
public static void main(String[] args) {
int age = 17;
try{
if(age<18){
throw new UnderAgeExceptions("EXCEPTION **: "); // parameterized
}
}catch( UnderAgeExceptions e){
e.printStackTrace();
}
System.out.println("hiii");
}
}
// complie, exception handle bhee hui aur program safe termiante hua