Mastering Java and Spring Boot - Live Course : https://bit.ly/TeluskoJavaLive
For More Queries WhatsApp or Call on : +919008963671

In this lecture we are discussing about custom Exception:

--Java provides a set of built-in exceptions that cover most of the common error scenarios in a program.
--there may be cases where we need to define our own exception class to represent an error specific to your program.

#1
How to create custom exception in java?

class MyException extends Exception{
// this class is own exception class defined by according to our need
}

-- MyException is a custom exception class that extends the Exception class.

#2
Now we want to pass some message
-- we need a constructor that takes a String message, which is passed to the parent constructor using the super keyword.
e.g -
public class MyException extends Exception {
public MyException(String message) {
super(message);
}
}

Note: we can add additional properties and methods to your custom exception class as needed, just like you would with any other Java class.

#3
As per need we can throw custom Exception
e.g
public class Main{
public static void main(String []args){
try{
throw new NavinException("This is my exception");
}catch(NavinException e){
System.out.println(e.getMessage());
}
}
}

class NavinException extends Exception{
public NavinException(String s) {
super(s);
}
}

Important Notes:
-- we can create a custom exception by extending the Throwable class in Java
-- we can create a custom exception by extending the Exception class in Java
-- we can create a custom exception by extending the Exception class or one of its subclasses, such as RuntimeException.

Recommended:
-- Generally recommended to create custom exceptions by extending the Exception class or one of its subclasses, such as RuntimeException.
This makes it easier to handle your exception in a specific way and ensures that it behaves like other exceptions in the Java platform.

-- it is not recommended create custom exception using Throwable, the Throwable class is typically used to create other types of exceptions,
such as errors and checked exceptions, rather than custom exceptions.

Github repo : https://github.com/navinreddy20/Javacode.git

Java:- https://bit.ly/JavaUdemyTelusko
Spring:- https://bit.ly/SpringUdemyTelusko

More Learning :

Java :- https://bit.ly/3x6rr0N
Python :- https://bit.ly/3GRc7JX
Django :- https://bit.ly/3MmoJK6

JavaScript :- https://bit.ly/3tiAlHo
Node JS :- https://bit.ly/3GT4liq

Rest Api :-https://bit.ly/3MjhZwt
Servlet :- https://bit.ly/3Q7eA7k
Spring Framework :- https://bit.ly/3xi7buh
Design Patterns in Java :- https://bit.ly/3MocXiq
Docker :- https://bit.ly/3xjWzLA

Blockchain Tutorial :- https://bit.ly/3NSbOkc
Corda Tutorial:- https://bit.ly/3thbUKa
Hyperledger Fabric :- https://bit.ly/38RZCRB

NoSQL Tutorial :- https://bit.ly/3aJpRuc
Mysql Tutorial :- https://bit.ly/3thpr4L

Data Structures using Java :- https://bit.ly/3MuJa7S
Git Tutorial :- https://bit.ly/3NXyCPu

Donation:
PayPal Id : navinreddy20
https://www.telusko.com