SlideShare a Scribd company logo
Exception Handling Part-II
Throw and Throws
• Throw-
• The throw keyword is used to transfer the
exception to someone else.
• Sometimes we can create Exception object
explicitly and we can hand over to the JVM
manually by using throw keyword.
• Example:
throw new ArithmeticException ();
Throw and Throws
• throw Example
• public class TestThrow {
• //defining a method
• public static void checkNum(int num) {
• if (num < 1) {
• throw new ArithmeticException("nNumber is negative, cannot calculate square");
• }
• else {
• System.out.println("Square of " + num + " is " + (num*num));
• }
• }
• //main method
• public static void main(String[] args) {
• TestThrow obj = new TestThrow();
• obj.checkNum(-3);
• System.out.println("Rest of the code..");
• }
• }
Throw and Throws
• Throws-
• Throws keyword is used to declare the exception
with method.
• We can use throws keyword to delegate the
responsibility of exception handling to the caller
method. Then caller method is responsible to handle
that exception.
• Example-
• void m1()throws ArithmeticException
• {
• m2();
• }
Throw and Throws
• throws Example
• public class TestThrows {
• //defining a method
• public static int divideNum(int m, int n) throws ArithmeticException {
• int div = m / n;
• return div;
• }
• //main method
• public static void main(String[] args) {
• TestThrows obj = new TestThrows();
• try {
• System.out.println(obj.divideNum(45, 0));
• }
• catch (ArithmeticException e){
• System.out.println("nNumber cannot be divided by 0");
• }
•
• System.out.println("Rest of the code..");
• }
• }
Throw and Throws
• Difference between Throw and Throws
throw throws
1 throw keyword is used to
throw an exception explicitly.
throws keyword is used to declare an exception.
2 throw is used inside method.
Example >
static void m(){
throw new
FileNotFoundException();
}
throws is used in method declaration.
Example >
static void m() throws
FileNotFoundException{
}
3 throw is always followed by
instanceof Exception class.
Example >
throw new
FileNotFoundException()
throws is always followed by name of
Exception class.
Example >
throws FileNotFoundException
4 throw can be used to throw
only one exception at time.
Example >
throw new
FileNotFoundException()
throws can be used to throw multiple exception
at time.
Example >
throws FileNotFoundException,
NullPointerException
and many more...
Custom Exception
• We can create our own Exception that is
known as custom exception or user defined
exception.
• Steps to create the custom exception
• 1. Create the new class.
• 2. The user defined exception class must
extend from java.lang.exception or
java.lang.RunTimeException class.
Custom Exception
• 3.While creating custom exception, prefer to
create an unchecked, Runtime exception than
a checked exception.
• 4.Every user defined exception class in which
parameterized constructor must be called
parameterized constructor of either
java.lang.Exception or
java.lang,RunTimeException class by using
super(String parameter always)
Custom Exception
• Example
• Package com.Test
• Public class InsufficientFundException extends
RuntimeException
• {
• private String message;
• public InsufficientFundException(String message)
• {
• super(message);
• }
}
Custom Exception
• Example
• Package com.Test
• Public class Account
• {
• private int balance=3000;
• public int getBalance()
• {
• return balance;
• }
Custom Exception
• Public void withDraw(int amount)
• {
• if (amount > balance)
• {
• throw new
InsufficientFundException(“InSufficient balance in
your account....”);
• }
• balance=balance-amount;
• }
• }
Custom Exception
• Package com.test
• Public class TestMain()
• {
• public static void main(String[] args)
• {
• Account acc=new Account();
• System.out.println(“Current
balance:”+acc.WithDraw(3500);
• System.out.println(“Current
Balance:”+acc.getBalance());
• }
• }
What is Exception propagation
• The process of sending exceptions from called
method to the calling method is called
exception propagation.
Method3()
Method2()
Method1()
Main()
What is Exception propagation
• Example:1
public class Example
{
void m() throws ArithmeticException
{
int x=50/0;
}
void n() throws ArithmeticException
{
m();
}
What is Exception propagation
• Example:1
void p() throws ArithmeticException
{
n();
}
Public static void main(string[] args) throws ArithmeticException
{
Example ex=new Example();
ex.p();
System.out.println(“normal Flow….”);
}
• Example 1 exception is not handled so it will
stop abnormally.
What is Exception propagation
Example:2
public class Test
{
void m() throws ArithmeticException
{
int x=50/0;
}
void n() throws ArithmeticException
{
m();
}
What is Exception propagation
Example:2
void p() throws ArithmeticException
{
try
{
n();
}
catch(ArithmeticException ae)
{
System.out.println(“Exception handled”);
System.out.println(ae.getMessage());
}
}
What is Exception propagation
Example:2
public static void main(string[] args)
{
Test test=new Test();
test.p();
System.out.println(“normal Flow….”);
}
Example:2 Exception is handled so it will print in console window
Exception handled
/ by Zero
Normal flow…………
Default exceptional handler
• If an exception raised in the method, JVM checks for
exception handling code in that method. If method
doesn’t contain any Exception handling code the JVM
terminate method abnormally and removes entry
from stack.
• This Process will be continue until Main() method.
• If Main() method also doesn’t contain exception
handling code the JVM terminate main() and remove
entry from stack.
• Default Exception Handler just print Exception
Information to the console.

More Related Content

PPTX
Unit II Java & J2EE regarding Java application development
PPTX
Java-Exception Handling Presentation. 2024
PPTX
Exception Handling In Java Presentation. 2024
PPTX
Java -Exception handlingunit-iv
PPTX
Chapter class 11 exception and errors-9.pptx
PDF
Java exceptions
PPT
Exception handling
PPT
Exception
Unit II Java & J2EE regarding Java application development
Java-Exception Handling Presentation. 2024
Exception Handling In Java Presentation. 2024
Java -Exception handlingunit-iv
Chapter class 11 exception and errors-9.pptx
Java exceptions
Exception handling
Exception

Similar to Core Java Concept Exception Handling Part-II.ppt (20)

PPT
Exception
PPT
Exception
PPT
Exception
PPT
Exception
PPT
Exception
PPT
Exception
PPT
Exception
PPT
Exception
PPTX
java.pptx
PPT
Java exception
PPTX
Java exception handling
PPTX
Module 4.pptxModule 4.pptxModuModule 4.pptxModule 4.pptxle 4.pptx
PDF
Exception handling
PDF
Exception handling
PPTX
PACKAGES, INTERFACES AND EXCEPTION HANDLING
PPT
Exception handling
PPTX
L14 exception handling
PPT
Exception handling
PPTX
Exception handling in java
PPTX
Exception handling in java
Exception
Exception
Exception
Exception
Exception
Exception
Exception
Exception
java.pptx
Java exception
Java exception handling
Module 4.pptxModule 4.pptxModuModule 4.pptxModule 4.pptxle 4.pptx
Exception handling
Exception handling
PACKAGES, INTERFACES AND EXCEPTION HANDLING
Exception handling
L14 exception handling
Exception handling
Exception handling in java
Exception handling in java
Ad

More from sonalipatil225940 (6)

DOCX
Its Concept of Collection _JAVA_Collection2.docx
DOCX
JAVA Programming-Lectures-1_javaFeatures (2).docx
PPTX
Swing component point are mentioned in PPT which helpgul for creating Java GU...
PPTX
Java Programming Environment,JDK,JRE,JVM.pptx
PPTX
Introduction To Java history, application, features.pptx
PPTX
Exception handling in java-PPT.pptx
Its Concept of Collection _JAVA_Collection2.docx
JAVA Programming-Lectures-1_javaFeatures (2).docx
Swing component point are mentioned in PPT which helpgul for creating Java GU...
Java Programming Environment,JDK,JRE,JVM.pptx
Introduction To Java history, application, features.pptx
Exception handling in java-PPT.pptx
Ad

Recently uploaded (20)

PPTX
Sustainable Sites - Green Building Construction
PPTX
UNIT 4 Total Quality Management .pptx
PPTX
Geodesy 1.pptx...............................................
PPTX
OOP with Java - Java Introduction (Basics)
PDF
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
PPTX
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
PPT
Project quality management in manufacturing
PDF
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
PDF
Automation-in-Manufacturing-Chapter-Introduction.pdf
PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PPT
introduction to datamining and warehousing
PPTX
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
PPTX
Construction Project Organization Group 2.pptx
PPTX
Internet of Things (IOT) - A guide to understanding
PDF
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
PDF
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
PDF
composite construction of structures.pdf
DOCX
573137875-Attendance-Management-System-original
PPTX
additive manufacturing of ss316l using mig welding
PDF
Digital Logic Computer Design lecture notes
Sustainable Sites - Green Building Construction
UNIT 4 Total Quality Management .pptx
Geodesy 1.pptx...............................................
OOP with Java - Java Introduction (Basics)
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
Project quality management in manufacturing
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
Automation-in-Manufacturing-Chapter-Introduction.pdf
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
introduction to datamining and warehousing
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
Construction Project Organization Group 2.pptx
Internet of Things (IOT) - A guide to understanding
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
composite construction of structures.pdf
573137875-Attendance-Management-System-original
additive manufacturing of ss316l using mig welding
Digital Logic Computer Design lecture notes

Core Java Concept Exception Handling Part-II.ppt

  • 2. Throw and Throws • Throw- • The throw keyword is used to transfer the exception to someone else. • Sometimes we can create Exception object explicitly and we can hand over to the JVM manually by using throw keyword. • Example: throw new ArithmeticException ();
  • 3. Throw and Throws • throw Example • public class TestThrow { • //defining a method • public static void checkNum(int num) { • if (num < 1) { • throw new ArithmeticException("nNumber is negative, cannot calculate square"); • } • else { • System.out.println("Square of " + num + " is " + (num*num)); • } • } • //main method • public static void main(String[] args) { • TestThrow obj = new TestThrow(); • obj.checkNum(-3); • System.out.println("Rest of the code.."); • } • }
  • 4. Throw and Throws • Throws- • Throws keyword is used to declare the exception with method. • We can use throws keyword to delegate the responsibility of exception handling to the caller method. Then caller method is responsible to handle that exception. • Example- • void m1()throws ArithmeticException • { • m2(); • }
  • 5. Throw and Throws • throws Example • public class TestThrows { • //defining a method • public static int divideNum(int m, int n) throws ArithmeticException { • int div = m / n; • return div; • } • //main method • public static void main(String[] args) { • TestThrows obj = new TestThrows(); • try { • System.out.println(obj.divideNum(45, 0)); • } • catch (ArithmeticException e){ • System.out.println("nNumber cannot be divided by 0"); • } • • System.out.println("Rest of the code.."); • } • }
  • 6. Throw and Throws • Difference between Throw and Throws throw throws 1 throw keyword is used to throw an exception explicitly. throws keyword is used to declare an exception. 2 throw is used inside method. Example > static void m(){ throw new FileNotFoundException(); } throws is used in method declaration. Example > static void m() throws FileNotFoundException{ } 3 throw is always followed by instanceof Exception class. Example > throw new FileNotFoundException() throws is always followed by name of Exception class. Example > throws FileNotFoundException 4 throw can be used to throw only one exception at time. Example > throw new FileNotFoundException() throws can be used to throw multiple exception at time. Example > throws FileNotFoundException, NullPointerException and many more...
  • 7. Custom Exception • We can create our own Exception that is known as custom exception or user defined exception. • Steps to create the custom exception • 1. Create the new class. • 2. The user defined exception class must extend from java.lang.exception or java.lang.RunTimeException class.
  • 8. Custom Exception • 3.While creating custom exception, prefer to create an unchecked, Runtime exception than a checked exception. • 4.Every user defined exception class in which parameterized constructor must be called parameterized constructor of either java.lang.Exception or java.lang,RunTimeException class by using super(String parameter always)
  • 9. Custom Exception • Example • Package com.Test • Public class InsufficientFundException extends RuntimeException • { • private String message; • public InsufficientFundException(String message) • { • super(message); • } }
  • 10. Custom Exception • Example • Package com.Test • Public class Account • { • private int balance=3000; • public int getBalance() • { • return balance; • }
  • 11. Custom Exception • Public void withDraw(int amount) • { • if (amount > balance) • { • throw new InsufficientFundException(“InSufficient balance in your account....”); • } • balance=balance-amount; • } • }
  • 12. Custom Exception • Package com.test • Public class TestMain() • { • public static void main(String[] args) • { • Account acc=new Account(); • System.out.println(“Current balance:”+acc.WithDraw(3500); • System.out.println(“Current Balance:”+acc.getBalance()); • } • }
  • 13. What is Exception propagation • The process of sending exceptions from called method to the calling method is called exception propagation. Method3() Method2() Method1() Main()
  • 14. What is Exception propagation • Example:1 public class Example { void m() throws ArithmeticException { int x=50/0; } void n() throws ArithmeticException { m(); }
  • 15. What is Exception propagation • Example:1 void p() throws ArithmeticException { n(); } Public static void main(string[] args) throws ArithmeticException { Example ex=new Example(); ex.p(); System.out.println(“normal Flow….”); } • Example 1 exception is not handled so it will stop abnormally.
  • 16. What is Exception propagation Example:2 public class Test { void m() throws ArithmeticException { int x=50/0; } void n() throws ArithmeticException { m(); }
  • 17. What is Exception propagation Example:2 void p() throws ArithmeticException { try { n(); } catch(ArithmeticException ae) { System.out.println(“Exception handled”); System.out.println(ae.getMessage()); } }
  • 18. What is Exception propagation Example:2 public static void main(string[] args) { Test test=new Test(); test.p(); System.out.println(“normal Flow….”); } Example:2 Exception is handled so it will print in console window Exception handled / by Zero Normal flow…………
  • 19. Default exceptional handler • If an exception raised in the method, JVM checks for exception handling code in that method. If method doesn’t contain any Exception handling code the JVM terminate method abnormally and removes entry from stack. • This Process will be continue until Main() method. • If Main() method also doesn’t contain exception handling code the JVM terminate main() and remove entry from stack. • Default Exception Handler just print Exception Information to the console.