Skip to main content

Exception Handling in Java

Exception Handling()


  • it is an event that stops the normal flow of execution of the program and terminates the program abnormally.
  • Exception always occurs at runtime.
  • There Is two type of Exception:
  1. Checked Exception.:-The exception which is checked by the compiler at the compilation time. Ex: IOException,ClassNotFoundException.
  2. Unchecked Exception: The Exception which Cant be checked by the compiler at the compilation time and directly occurs at runtime is called an unchecked Exception.




Ex:NullPointerException,StringIndexOutOfBoundsException.
Error: Error is the type of unchecked Exception which occurs due to programmers mistake, end-user input mistake, resource unavailability or network problem etc.
OutOfMemoryError
StackOverFlowError
etc.



5 keywords to handle Exception:
try
catch
finally
throw
throws
possible combination:
  1. try-catch
  2. try-catch-catch
  3. try-catch-finally
  4. try finally
  5. try-catch-catch-finally


try:
the statement which may throw errors.
Catch
this block handles the error and we can write alternate code to run.




Finally:
this block contains code which must run at any cost even if the error is generated or not.
________________________________________________________________________________
Whenever exception occurs in try block the corresponding matching catch block will be executed.


If the exception doesn't occur in the try block, catch block will not be executed but finally will execute.
There are three cases only finally will not execute
1.When the exception occurs before the try block.
2. When JVM stopped explicitly by the System.exit(0);
3. When the exception is not handled by any of the corresponding catch blocks ..finally will not be executed.


In child class exception is occurring or there is a possibility of occurrence is there we can catch the parent class exception the catch block.




Throws:
it is used to handle the exception by delegating the responsibility of the exception handling to the caller method.
It is more useful in checked Exception.
We can throw parent class exception also in place of child class exception.
It is used to transfer(Delegate ) the responsibility to the caller method.
We can handle multiple exceptions simultaneously by using by using throws.






Throw :
In child class exception is occurring or there is a possibility of occurrence is there we can catch the parent throw keyword is used to hand over user-defined exception to the JVM.
There are two approaches to create user-defined Exception.


1. By default Constructor
2.By Argumented Constructor.




User Defined Constructor are of Two type:
1.Checked: It is created by using the exception class.
2.Unchecked: Unchecked User-defined Exception are created by using runtime exception class.

Comments

Popular posts from this blog

13 websites to register your free domain

Register your Free Domain Now!! 1)  .tk Dot TK is a FREE domain registry for websites on the Internet. It has exactly the same power as other domain extensions, but it’s free! Because it’s free, millions of others have been using .TK domains since 2001 – which makes .TK powerful and very recognizable.  Your website will be like www.yourdomainname.tk . It is free for 1 year. It’s a ccTLD domain whixh having the abbreviation  Tokelau. To create a .tk domain, Visit   www.dot.tk 2) co.cc Co.cc is completely free domain which is mostly used by blogspot bloggers because of it’s easy to use DNS system. Creating a co.cc for blogger is simple ( for instructions- “click here”). Your website will be like www.yourdomainname.co.cc . To create a .co.cc domain, visit www.co.cc 3)   co.nr co.nr is too like co.cc. Your website will be like  www.yourdomainname.co.nr . You can add it for blogger also.. To create a .co.cc domain, vi...

How to Put Google Adsense Below Post Title in Blogger?

Adsense is used by majority of expert bloggers for their website monetization because it is a cookie based contextual advertising system that shows targeted ads relevant to the content and reader. As bloggers are paid on per click basis, they try various ad placements on the blog to  increase the revenue  and get maximum clicks on the ad units. Well, on some blogs, you might have seen Adsense ad units placed below the post title. Do you know why? It is because the area just below the post title gets the most exposure and is the best place to put AdSense ad units to increase  Click Through Rate (CTR). Even though ads below post title work like a charm but this doesn’t mean that it will work for you as well. If you want to find out the best AdSense ads placement for your blog, try experimenting by placing ads at various locations such as header, sidebar, footer, etc. You can try other  blog monetization methods  as well to effectively monetize y...

python program to Print Starting Series OF Indian Mobile Number for a State or operator or both

import requests import urllib.request import time from bs4 import BeautifulSoup as bs import re url = ' https://en.wikipedia.org/wiki/Mobile_telephone_numbering_in_India' state_to_extract = "UE" #if set to None all state is considered telecom_to_extracted = None #if set to none all operator from particular city is extracted response = requests . get(url) print (response) soup = bs(response . text, "html.parser" ) one_a_tag = soup . findAll( 'tr' )[ 35 :] lst = [] for k in one_a_tag: s = k . findAll( 'td' ) limit = len (s) i = 0 while True : if i == limit: break no = s[i] . text i += 1 if i == limit: break operator = s[i] . text i += 1 if i == limit: break state = s[i] . text i += 1 if i == limit: break res = f "{no} {operator} {state}" if state_to_extract is None : if telecom_to_extracted is None : lst . append(no) elif telecom_to_e...