No video

#82 Ducking Exception using throws in Java

  Рет қаралды 66,936

Telusko

Telusko

Күн бұрын

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

Пікірлер: 25
@harsh-ox9gy
@harsh-ox9gy Жыл бұрын
best java tutorial series!
@malinigowda5586
@malinigowda5586 6 ай бұрын
best java tutorial series! Thank you so much Navin
@Giri14_18
@Giri14_18 9 ай бұрын
sir the lectures are just awesome . sir are you from south india ? telugu states ?
@manasdhanpawde496
@manasdhanpawde496 8 ай бұрын
yes,he has reddy in his name
@Rhanjag
@Rhanjag 25 күн бұрын
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.
@parthisanjay3252
@parthisanjay3252 5 ай бұрын
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 4 ай бұрын
It will jump to catch block to handle the exception ...then the execution will continue and d() will also be executed
@parthisanjay3252
@parthisanjay3252 4 ай бұрын
@@adarshveerabathini8550 No bro, i tried d( ) will not execute
@adarshveerabathini8550
@adarshveerabathini8550 4 ай бұрын
If u want the d() method to be executed u need to place it in finally block
@adarshveerabathini8550
@adarshveerabathini8550 4 ай бұрын
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??
@jackfrost8969
@jackfrost8969 Ай бұрын
you have not taught static {} code up until now
@indiandeveloper8614
@indiandeveloper8614 9 ай бұрын
In Exception you are going so fast
@HN-bn6vv
@HN-bn6vv 9 ай бұрын
Yes, but you can adjust speed 😀
@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
@kochi_02_abhinavtongale73
@kochi_02_abhinavtongale73 Жыл бұрын
👍👍👍👍
@jayanthipenugonda7274
@jayanthipenugonda7274 9 ай бұрын
I remember that Class.forName was seen previously in one of the videos of this series. If anyone knows please tell me.
@virendersinghnegi5009
@virendersinghnegi5009 Ай бұрын
I don't think so he taught us this in the past.
@tusharburman2831
@tusharburman2831 29 күн бұрын
no i guess class,forName he didnt taught us in the past videos
@LifeAt1004_
@LifeAt1004_ 16 күн бұрын
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 13 күн бұрын
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)
@ooogabooga5111
@ooogabooga5111 4 ай бұрын
but this only works for checked exceptions, what if I want to force unchecked exceptions to be handled ?
@Nikitaaaaa17
@Nikitaaaaa17 28 күн бұрын
Accenture pdf
@ManishKumar-zt5sk
@ManishKumar-zt5sk 3 ай бұрын
Taklusko
@devanshsharma8543
@devanshsharma8543 3 ай бұрын
bro didn't hesitate
#83 User Input using BufferedReader and Scanner in Java
11:48
#81 Custom Exception in Java
3:30
Telusko
Рет қаралды 94 М.
مسبح السرير #قصير
00:19
سكتشات وحركات
Рет қаралды 2,1 МЛН
When you discover a family secret
00:59
im_siowei
Рет қаралды 24 МЛН
طردت النملة من المنزل😡 ماذا فعل؟🥲
00:25
Cool Tool SHORTS Arabic
Рет қаралды 24 МЛН
English or Spanish 🤣
00:16
GL Show
Рет қаралды 8 МЛН
Learn Any Programming Language In 3 Hours!
22:37
Code With Huw
Рет қаралды 353 М.
I've been using Redis wrong this whole time...
20:53
Dreams of Code
Рет қаралды 354 М.
#85 Threads in Java
5:13
Telusko
Рет қаралды 138 М.
#76  What is Exception in Java
5:19
Telusko
Рет қаралды 73 М.
Checked vs. Unchecked Exceptions in Java Tutorial - What's The Difference?
10:14
Fastest Way to Learn ANY Programming Language: 80-20 rule
8:24
Sahil & Sarra
Рет қаралды 832 М.
#80 Exception throw keyword in Java
5:35
Telusko
Рет қаралды 102 М.
مسبح السرير #قصير
00:19
سكتشات وحركات
Рет қаралды 2,1 МЛН