Object Oriented Programming: Unit III: Exception Handling and Multithreading

Using throw

with Example Exception Handling Java Programs

For explicitly throwing the exception, the keyword throw is used. The keyword throw is normally used within a method. We can not throw multiple exceptions using throw.

Using throw

• For explicitly throwing the exception, the keyword throw is used. The keyword throw is normally used within a method. We can not throw multiple exceptions using throw.

Java Programming Example

class Exception Throw

{

static void fun(int a,int b)

{

int c;

if(b==0)

throw new ArithmeticException("Divide By Zero!!!");

else

c=a/b;

}

public static void main(String args[])

{

int a=5;

fun(a,0);

}

}

Output

Exception in thread "main" java.lang.ArithmeticException: Divide By Zero!!!

at ExceptionThrow.fun(ExceptionThrow.java:7)

at ExceptionThrow.main(ExceptionThrow.java:14)

Difference between throw and throws


Object Oriented Programming: Unit III: Exception Handling and Multithreading : Tag: : with Example Exception Handling Java Programs - Using throw


Object Oriented Programming: Unit III: Exception Handling and Multithreading



Under Subject


Object Oriented Programming

CS3391 3rd Semester CSE Dept | 2021 Regulation | 3rd Semester CSE Dept 2021 Regulation



Related Subjects


Discrete Mathematics

MA3354 3rd Semester CSE Dept | 2021 Regulation | 3rd Semester CSE Dept 2021 Regulation


Digital Principles and Computer Organization

CS3351 3rd Semester CSE Dept | 2021 Regulation | 3rd Semester CSE Dept 2021 Regulation


Foundation of Data Science

CS3352 3rd Semester CSE Dept | 2021 Regulation | 3rd Semester CSE Dept 2021 Regulation


Data Structure

CS3301 3rd Semester CSE Dept | 2021 Regulation | 3rd Semester CSE Dept 2021 Regulation


Object Oriented Programming

CS3391 3rd Semester CSE Dept | 2021 Regulation | 3rd Semester CSE Dept 2021 Regulation