What is Exception Handling in Java | Full Stack With Java

Exception in Java | Exception Handling in Java | What is Exception in Java? | What is Exception Handling? |

Exception in Java : – Exception Make Our Program Halt Abruptly When a Bad User input is Given.

Example 1

public class A   {
public static void main ( String[] args ) {
int x=10;
int y=0;
int z=x/y;          // Error
System.out.println(z);
System.out.println("WELCOME");
}
}

Exception Handling in Java

Exception Handling in Java : –

  • To handle Exception We Use try catch Block.
  • If Any Line of Code in try Block Causes Exception than try Block Will Create Exception Object and that Object Address is Given to Catch Block.
  • catch Block Will Now Suppress the Exception and Hence The Further Code in the Program Will Continue to Execute.

Example 2

public class A   {
public static void main ( String[] args ) {
try {
int x=10;
int y=0;
int z=x/y;          // Error
System.out.println(z);
} catch ( Exception e ) {
System.out.println(e);
}
System.out.println("WELCOME");
}
}
Output : WELCOME

Leave a Comment