SlideShare a Scribd company logo
2
Most read
3/18/2019 Creating your own exception: Your Guided Course Template
https://canvas.instructure.com/courses/1480238/pages/creating-your-own-exception?module_item_id=21012890 1/2
Crea ng your own excep on
Types of Exception in Java
Built-in Exceptions
User-Defined Exceptions
Built-in exceptions:
Built-in exceptions are the exceptions which are available in Java libraries. These exceptions are suitable
to explain certain error situations.
User-Defined Exceptions
User Defined Exception or custom exception is creating your own exception class and throws that
exception using ‘throw’ keyword. This can be done by extending the class Exception.
Example of java custom exception:
class InvalidAgeException extends Exception
{
InvalidAgeException(String s)
{
super(s);
}
}
class TestCustomException1
{
static void validate(int age)throws InvalidAgeException
{
if(age<18)
throw new InvalidAgeException("not valid");
else
System.out.println("welcome to vote");
}
public static void main(String args[])
3/18/2019 Creating your own exception: Your Guided Course Template
https://canvas.instructure.com/courses/1480238/pages/creating-your-own-exception?module_item_id=21012890 2/2
{
try
{
validate(13);
}catch(Exception m)
{
System.out.println("Exception occured: "+m);
}
System.out.println("rest of the code...");
}
}
Output:Exception occured: InvalidAgeException:not valid
rest of the code...

More Related Content

PPTX
MULTI THREADING IN JAVA
PPT
Exception Handling in JAVA
PPTX
PPTX
Java exception handling
PPTX
This keyword in java
PPTX
[OOP - Lec 07] Access Specifiers
MULTI THREADING IN JAVA
Exception Handling in JAVA
Java exception handling
This keyword in java
[OOP - Lec 07] Access Specifiers

What's hot (20)

PPTX
Java abstract class & abstract methods
PPTX
Control structures in java
PPTX
Classes objects in java
PPS
Java Exception handling
PPT
Java-java virtual machine
PPTX
Final keyword in java
PPT
Final keyword in java
PPTX
JAVA AWT
PPS
Introduction to class in java
PPTX
Event Handling in java
PPTX
Inheritance in java
PPTX
Exception handling
PPTX
Java tokens
PPTX
Java package
PDF
Built in exceptions
PPTX
Inline function
PDF
Python exception handling
PPT
Awt controls ppt
PPTX
Event handling
PPTX
Arrays in Java
Java abstract class & abstract methods
Control structures in java
Classes objects in java
Java Exception handling
Java-java virtual machine
Final keyword in java
Final keyword in java
JAVA AWT
Introduction to class in java
Event Handling in java
Inheritance in java
Exception handling
Java tokens
Java package
Built in exceptions
Inline function
Python exception handling
Awt controls ppt
Event handling
Arrays in Java
Ad

Similar to Creating your own exception (20)

PDF
Types of Exceptionsfsfsfsdfsdfdsfsfsdfsd
PPTX
Exception handling
PPTX
Exception Handling
PPT
Exception and ErrorHandling in Java .ppt
PPTX
Session 13 - Exception handling - continued
PPSX
Exception Handling - Continued
PPTX
Lecture 22
PDF
Exception handling basic
ODP
Exception Handling In Java
ODP
Exception Handling In Java 15734
PDF
Classes, objects, methods, constructors, this keyword in java
PPT
PPTX
exception handling in java
PPTX
unit 4 msbte syallbus for sem 4 2024-2025
PPT
Exception Handling
PPTX
UNIT 2.pptx
PPT
Computer Object Oriented Programming - Chapter 4 - Excption Handling.ppt
DOCX
Unit5 java
PPT
Exception handling
Types of Exceptionsfsfsfsdfsdfdsfsfsdfsd
Exception handling
Exception Handling
Exception and ErrorHandling in Java .ppt
Session 13 - Exception handling - continued
Exception Handling - Continued
Lecture 22
Exception handling basic
Exception Handling In Java
Exception Handling In Java 15734
Classes, objects, methods, constructors, this keyword in java
exception handling in java
unit 4 msbte syallbus for sem 4 2024-2025
Exception Handling
UNIT 2.pptx
Computer Object Oriented Programming - Chapter 4 - Excption Handling.ppt
Unit5 java
Exception handling
Ad

More from TharuniDiddekunta (14)

PDF
String class
PDF
Packages access protection, importing packages
PDF
Interfaces in java
PDF
Inheritance used in java
PDF
Operators, control statements represented in java
PDF
Arrays in java
PPTX
Software Metrics (Testing)
PPTX
unit 3 Design 1
PPTX
Unit 4 testing
PPTX
risk managment and quality
PPTX
PPT
Network layer
PPTX
Transport layer and Application layer
PPT
Congection control and Internet working
String class
Packages access protection, importing packages
Interfaces in java
Inheritance used in java
Operators, control statements represented in java
Arrays in java
Software Metrics (Testing)
unit 3 Design 1
Unit 4 testing
risk managment and quality
Network layer
Transport layer and Application layer
Congection control and Internet working

Recently uploaded (20)

PPTX
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
PPTX
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
PPTX
Sustainable Sites - Green Building Construction
PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PPTX
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
PDF
composite construction of structures.pdf
PPTX
Welding lecture in detail for understanding
PDF
Model Code of Practice - Construction Work - 21102022 .pdf
PPTX
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
PDF
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
DOCX
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
PPTX
Lecture Notes Electrical Wiring System Components
PPT
Project quality management in manufacturing
PPTX
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
PDF
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
PPTX
UNIT 4 Total Quality Management .pptx
PPTX
CH1 Production IntroductoryConcepts.pptx
PDF
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
PPTX
Geodesy 1.pptx...............................................
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
Sustainable Sites - Green Building Construction
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
composite construction of structures.pdf
Welding lecture in detail for understanding
Model Code of Practice - Construction Work - 21102022 .pdf
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
Embodied AI: Ushering in the Next Era of Intelligent Systems
Lecture Notes Electrical Wiring System Components
Project quality management in manufacturing
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
UNIT 4 Total Quality Management .pptx
CH1 Production IntroductoryConcepts.pptx
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
Geodesy 1.pptx...............................................

Creating your own exception

  • 1. 3/18/2019 Creating your own exception: Your Guided Course Template https://canvas.instructure.com/courses/1480238/pages/creating-your-own-exception?module_item_id=21012890 1/2 Crea ng your own excep on Types of Exception in Java Built-in Exceptions User-Defined Exceptions Built-in exceptions: Built-in exceptions are the exceptions which are available in Java libraries. These exceptions are suitable to explain certain error situations. User-Defined Exceptions User Defined Exception or custom exception is creating your own exception class and throws that exception using ‘throw’ keyword. This can be done by extending the class Exception. Example of java custom exception: class InvalidAgeException extends Exception { InvalidAgeException(String s) { super(s); } } class TestCustomException1 { static void validate(int age)throws InvalidAgeException { if(age<18) throw new InvalidAgeException("not valid"); else System.out.println("welcome to vote"); } public static void main(String args[])
  • 2. 3/18/2019 Creating your own exception: Your Guided Course Template https://canvas.instructure.com/courses/1480238/pages/creating-your-own-exception?module_item_id=21012890 2/2 { try { validate(13); }catch(Exception m) { System.out.println("Exception occured: "+m); } System.out.println("rest of the code..."); } } Output:Exception occured: InvalidAgeException:not valid rest of the code...