Тёмный

#82 Ducking Exception using throws in Java 

Telusko
Подписаться 2,4 млн
Просмотров 75 тыс.
50% 1

Check out our courses:
Enterprise Java Spring Microservices: go.telusko.com...
Coupon: TELUSKO10 (10% Discount)
Master Java Spring Development : go.telusko.com...
Coupon: TELUSKO20 (20% Discount)
For More Queries WhatsApp or Call on : +919008963671
Udemy Courses:
Spring: go.telusko.com...
Java:- go.telusko.com...
Java Spring:- go.telusko.com...
Java For Programmers:- go.telusko.com...
Python : go.telusko.com...
Git : go.telusko.com...
Docker : go.telusko.com...
website : courses.telusk...
In this lecture, we are discussing ducking the exception using the throws keyword:
#1
-- throws is not plural of throw
-- throws keyword is used in method signatures to indicate that the method may throw certain types of exceptions.
but does not handle them directly
-- the caller of the method is responsible for catching and handling the exception.
#2
Best case to use the throws keyword instead of handling with try and catch:
-- throws keyword can be an appropriate way to handle exceptions in certain cases, such as when a method is part of a larger
program and the exception handling is being handled at a higher level. It can also be useful when creating reusable code that may
be used in a variety of contexts, where it's not clear how the exceptions should be handled.
e.g
suppose you have three methods c , b and a and both have same Arithmetic Exception and b and a method call from c method.
in this case you can duck the exceptions and handle in the c methods.
void c(){
try{
a();
b();
}
catch(ArithmeticException e){
}
}
void a() throws ArithmeticException{}
void b() throws ArithmeticException{}
-- ducking exception most recommended for checked exception than unchecked exception
-- throws keyword in Java is typically used to declare that a method may throw one or more
checked exceptions. Checked exceptions are exceptions that must be either caught or declared in the method
signature using the throws keyword. Examples of checked exceptions in Java include IOException, SQLException,
and ClassNotFoundException.
Syntax to throws one or more Exception:
public void myMethod() throws IOException, SQLException {
// Method code that may throw either an IOException or a SQLException
}
Important: it's best to handle exceptions at the lowest possible level of the code where
suppose we have main() method inside main method we call c() method and inside c method we call a() method and in
a() method exception originate and we cannot handle with try and catch then propagate to c() method and if not handled propagate to
main method and if not handled in main method then the default exception handler handles the exception and abnormally terminate the program.
#3
In java how exceptions propagate
In Java, when an exception is thrown but not handled by the current method, the exception is propagated up
the call stack until it is either caught and handled by a try-catch block, or it reaches the top level of
the program and the program terminates.
The order of transferring unhandled exceptions in Java is as follows:
The current method throws an exception.
i)If there is a try-catch block within the current method that can handle the exception,
the exception is caught and handled within that block. Control then passes to the code
following the catch block.
ii)If there is no try-catch block within the current method that can handle the exception,
the exception is propagated up to the calling method.
iii)Steps 2 and 3 repeat until either the exception is caught and handled by a try-catch block,
or it reaches the top level of the program.
iv)If the exception reaches the top level of the program without being caught and handled,
v)in this case
If an exception is not handled by any method in the call stack, and there is no catch block that can handle the exception,
then the default exception handler in Java is called to handle the exception.
-- default exception handler in Java is part of the JVM (Java Virtual Machine) and is responsible for printing the exception
information to the console or to a log file, and terminating the program.
-- the default exception handler is called, it prints a stack trace that shows the sequence of method calls that led up to the exception,
as well as any other relevant information about the exception.
Github repo : github.com/nav...
More Learning :
Java :- bit.ly/3x6rr0N
Python :- bit.ly/3GRc7JX
Django :- bit.ly/3MmoJK6
Rest Api :-bit.ly/3MjhZwt
Servlet :- bit.ly/3Q7eA7k
Spring Framework :- bit.ly/3xi7buh
Design Patterns in Java :- bit.ly/3MocXiq
Docker :- bit.ly/3xjWzLA
Donation:
PayPal Id : navinreddy20
www.telusko.com

Опубликовано:

 

16 окт 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 27   
@harsh-ox9gy
@harsh-ox9gy Год назад
best java tutorial series!
@suneethamudragada
@suneethamudragada Месяц назад
Very usefull for us in this video I learned the throws very easily iam very confusing in my institution while explaining about this. They are not explained just they said keep throws keyword now I got clarity about this thank you very much .
@malinigowda5586
@malinigowda5586 7 месяцев назад
best java tutorial series! Thank you so much Navin
@Rhanjag
@Rhanjag 2 месяца назад
Hi Naveen, it's not thought in the series for Class.forName. Can you add a series here? It would be really helpful to us.
@Giri14_18
@Giri14_18 10 месяцев назад
sir the lectures are just awesome . sir are you from south india ? telugu states ?
@manasdhanpawde496
@manasdhanpawde496 10 месяцев назад
yes,he has reddy in his name
@parthisanjay3252
@parthisanjay3252 6 месяцев назад
public void b( ){ try{ c( ); d( ); } catch( ){ } in the above case b( ) method called both methods c( ) and d( ) with try block. If c( ) throws exception means execution will jumps to catch block so d( ) will not execute right. }
@adarshveerabathini8550
@adarshveerabathini8550 6 месяцев назад
It will jump to catch block to handle the exception ...then the execution will continue and d() will also be executed
@parthisanjay3252
@parthisanjay3252 6 месяцев назад
@@adarshveerabathini8550 No bro, i tried d( ) will not execute
@adarshveerabathini8550
@adarshveerabathini8550 6 месяцев назад
If u want the d() method to be executed u need to place it in finally block
@adarshveerabathini8550
@adarshveerabathini8550 6 месяцев назад
Once an exception is raised it will directly jumps to catch block by skipping all the remaining lines so....may be it will skip d()
@bipinkori4060
@bipinkori4060 Год назад
My main method throws AgeNotFoundException and JVM returns the error. why this soo??
@mdmusharraf7045
@mdmusharraf7045 28 дней назад
come here after wasting 1 hr in paid batch . Nice explanation 🧡💛💛
@LifeAt1004_
@LifeAt1004_ 2 месяца назад
Can Someone tell what is wrong with my code public class ThrowsKeyword { public static void show () throws ClassNotFoundException( ) { Class.forName(ThrowKeyword); } public static void main(String[] args) { try {show();} catch(ClassNotFoundException ce){ System.out.println("Class not found"); } } }
@dudisaicharan
@dudisaicharan 2 месяца назад
public class ThrowsKeyword { public static void show () throws ClassNotFoundException { Class.forName("ThrowKeyword"); } public static void main(String[] args) { try { show(); } catch(ClassNotFoundException ce){ System.out.println("Class not found"); } } } just small syntax error ,every thing looks fine with u. Error->class.forname(" ") throws ClassNotFoundException (its a class not a method,dont use brackets)
@jayanthipenugonda7274
@jayanthipenugonda7274 10 месяцев назад
I remember that Class.forName was seen previously in one of the videos of this series. If anyone knows please tell me.
@virendersinghnegi5009
@virendersinghnegi5009 2 месяца назад
I don't think so he taught us this in the past.
@tusharburman2831
@tusharburman2831 2 месяца назад
no i guess class,forName he didnt taught us in the past videos
@bipinkori4060
@bipinkori4060 Год назад
package ExceptionPractise; public class Voting { public static void vote(int age) throws AgeNotFoundException { if(age>18){ System.out.println("Eligible for voting"); } else throw new AgeNotFoundException("Not elegible"); } public static void main(String[] args) throws AgeNotFoundException{ vote(10); }//Main ends here }//class ends here
@jackfrost8969
@jackfrost8969 3 месяца назад
you have not taught static {} code up until now
@ooogabooga5111
@ooogabooga5111 5 месяцев назад
but this only works for checked exceptions, what if I want to force unchecked exceptions to be handled ?
@indiandeveloper8614
@indiandeveloper8614 11 месяцев назад
In Exception you are going so fast
@HN-bn6vv
@HN-bn6vv 11 месяцев назад
Yes, but you can adjust speed 😀
@kochi_02_abhinavtongale73
@kochi_02_abhinavtongale73 Год назад
👍👍👍👍
@Nikitaaaaa17
@Nikitaaaaa17 2 месяца назад
Accenture pdf
@ManishKumar-zt5sk
@ManishKumar-zt5sk 5 месяцев назад
Taklusko
@devanshsharma8543
@devanshsharma8543 4 месяца назад
bro didn't hesitate
Далее
#83 User Input using BufferedReader and Scanner in Java
11:48
#95 Comparator vs Comparable in Java
15:43
Просмотров 190 тыс.
#76  What is Exception in Java
5:19
Просмотров 85 тыс.
Everything Worth Knowing In Unity (Part 1)
9:14
Просмотров 3 тыс.
#85 Threads in Java
5:13
Просмотров 154 тыс.
Exception Handling in Java Tutorial
13:20
Просмотров 396 тыс.
Functional Interface | Lambda Expression in Java
13:56
Просмотров 153 тыс.