[!INDEX]
- Definition ( 3 points ) + divide /0 mt().
- Example code arithmetic ex in calling mt()
- ex. handle in calling class + try-catch ( above code )
- ex. handle in caller class + try-catch ( above code )
- exception flow theory ( 5 points )
- throw keyword ( 8 points )
- Syntax of throw.
- Throwing r/t e/x, + no try-catch + error ( student age )
- above code + try-catch + error ( above code )
- above code + try-catch + no error ( above code )
- Approach
1. Definition ( 3 points ) + divide /0 mt().
[!NOTE]
- used to explicitly throw an exception from your code.
- When you use
throw, you're manually creating an exception object and handing it off to the Java runtime (JVM). The thrown exception can then be caught by atry-catchblock, or it will propagate up the call stack until it's caught or the program terminates.- if it is not handle , jvm pass to DEH and program terminate abnormally
2. Example code arithmetic ex. in calling mt()
package package2;
public class Main1 {
public static void main(String[] args) {
Main1 m1 = new Main1();
m1.divide();
System.out.println("hello");
}
void divide(){
int a=100, b=0, c;
c=a/b;
System.out.println(c);
}
}
Exception in thread "main" java.lang.ArithmeticException: / by zero
at package2.Main1.divide(Main1.java:10)
at package2.Main1.main(Main1.java:5)
3. ex. handle in calling class + try-catch ( above code )
package package2;
public class Main1 {
public static void main(String[] args) {
Main1 t1 = new Main1();
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();
}
}
}
//hello
4. ex. handle in caller class + try-catch ( above code )
package package2;
public class Main1 {
public static void main(String[] args) {
Main1 t1 = new Main1();
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);
}
}
//hello
5. exception flow theory ( 5 points )
[!NOTE]
- एक्सेप्शन आने पर divide mt object create करेगा और
- JVM usko detect karke चेक करेगा की divide mt ने exception handle करा है की nahe.
- if no then jvm immediatly terminate the divide method.
- फिर JVM us object ko , caller (main) method में pass kar deyga aur चेक करेगा की exception handle करा है की nahe if no
- तो JVM वो डिफ़ॉल्ट एक्सेप्शन हैंडल (DEH) को पास कर deyga और वो फिर उसको हैंडल करेगा और प्रोग्राम अब्नोर्मल्ली टर्मिनेट हो जाएगा |
- can use try catch , in both method , but prefers in the divide method
6. throw keyword ( 8 points )
[!NOTE]
- explicitly throw an exception from your code.
- manually creating an exception object and handing it off to the Java runtime (JVM).
- The thrown exception can then be caught by a
try-catchblock, or it will propagate up the call stack until it's caught or the program terminates.- create and throw both checked and unchecked exceptions.
- Single exception at a time
- Used in methods
- prefer for unchecked exception
- only using this still make the program terminate abnormally.
7. Syntax of throw:
throw new ExceptionType("Error message");
8. Throwing r/t e/x, + no try-catch + error ( student age )
public class Main1 {
int age;
public static void main(String[] args) {
System.out.println("age is entered");
int age = 17 ;
Checkage ca = new Checkage();
ca.studentage(age);
System.out.println("code flow ok in parent clas ");
}
}
class Checkage{
void studentage(int x){
if(x<18){
throw new ArithmeticException("age is less");
}
else{
System.out.println("age ok");
}
System.out.println("code flow ok in child clas ");
}
}
age is entered
Exception in thread "main" java.lang.ArithmeticException: age is less
at package2.Checkage.studentage(Main1.java:17)
at package2.Main1.main(Main1.java:10)
9. above code + try-catch + error ( above code )
public class Main1 {
int age;
public static void main(String[] args) {
System.out.println("age is entered");
int age = 17 ;
Checkage ca = new Checkage();
try{
ca.studentage(age);
}
catch(ArithmeticException e)
{
e.printStackTrace();
}
System.out.println("code flow ok in parent class ");
}
}
class Checkage{
void studentage(int x){
if(x<18){
throw new ArithmeticException("age is less");
}
else{
System.out.println("age ok");
}
System.out.println("code flow ok in child clas ");
}
}
age is entered
java.lang.ArithmeticException: age is less
at package2.Checkage.studentage(Main1.java:24)
at package2.Main1.main(Main1.java:12)
code flow ok in parent clas
10. above code + try-catch + no error
public class Main1 {
int age;
public static void main(String[] args) {
System.out.println("age is entered");
int age = 19 ;
Checkage ca = new Checkage();
try{
ca.studentage(age);
}
catch(ArithmeticException e)
{
e.printStackTrace();
}
System.out.println("code flow ok in parent clas ");
}
}
class Checkage{
void studentage(int x){
if(x<18){
throw new ArithmeticException("age is less");
}
else{
System.out.println("age ok");
}
System.out.println("code flow ok in child clas ");
}
}
age is entered
age ok
code flow ok in child clas
code flow ok in parent clas
11. Approach
[!approach]
- call method (studentage) of other class (Checkage) using try-catch
- check and throw exception in studentage
image