What is the purpose of try?
We should maintain all risky code inside the try block.
What is the purpose of catch block?
We have to maintain all Exception Handling code
inside the catch block.
Is try with multiple catch block is possible?
The way of handling an exception is varied from exception to exception compulsory we have to write a separate catch block for every exception.
Hence try will multiple catch block is possible and it is recommended to use.
Example:
try{
//Risky code
}
catch[IOException e]
{
//Hndling code for IOException
}
catch[ArithmeticException e]
{
//handling code for AE
}
catch[NullPointerExcetpion e]
{
// handling code for NPE
}
catch[Exception e]
{
//default exception handling code
}
If try with multiple catch block present is order of catch blocks important in which order we have to take?
If try with multiple catch block present then the order of catch block is very important it should be from child to parent but not from parent to child.
The reason to use try/except is when you have a code block to execute that will sometimes run correctly and sometimes not, depending on conditions you can’t foresee at the time you’re writing the code.
For example, when you are running code that fetches data from a website, you may run the code when you don’t have a network connection or when the external website is temporarily not responding. If your program can still do something useful in those situations, you would like to handle the exception and have the rest of your code execute.
As another example, suppose you have fetched some nested data from a website into a dictionary d. When you try to extract specific elements, some may be missing: d may not include a particular key, for example. If you anticipate a particular key potentially not being present, you could write an if..else check to take care of it.
if somekey in d: # it's there; extract the data extract_data[d] else: skip_this_one[d]
However, if you’re extracting lots of different data, it can get tedious to check for all of them. You can wrap all the data extraction in a try/except.
try: extract_data[d] except: skip_this_one[d]
It’s considered poor practice to catch all exceptions this way. Instead, python provides a mechanism to specify just certain kinds of exceptions that you’ll catch [for example, just catching exceptions of type KeyError, which happens when a key is missing from a dictionary.
try: extract_data[d] except KeyError: skip_this_one[d]
We won’t go into more details of exception handling in this introductory course. Check out the official python tutorial section on error handling if you’re interested.
You have attempted of activities on this page