MBAData Science & AnalyticsDoctorate Software & Tech AI | ML MarketingManagement
Professional Certificate Programme in HR Management and AnalyticsPost Graduate Certificate in Product ManagementExecutive Post Graduate Program in Healthcare ManagementExecutive PG Programme in Human Resource ManagementMBA in International Finance (integrated with ACCA, UK)Global Master Certificate in Integrated Supply Chain ManagementAdvanced General Management ProgramManagement EssentialsLeadership and Management in New Age BusinessProduct Management Online Certificate ProgramStrategic Human Resources Leadership Cornell Certificate ProgramHuman Resources Management Certificate Program for Indian ExecutivesGlobal Professional Certificate in Effective Leadership and ManagementCSM® Certification TrainingCSPO® Certification TrainingLeading SAFe® 5.1 Training (SAFe® Agilist Certification)SAFe® 5.1 POPM CertificationSAFe® 5.1 Scrum Master Certification (SSM)Implementing SAFe® 5.1 with SPC CertificationSAFe® 5 Release Train Engineer (RTE) CertificationPMP® Certification TrainingPRINCE2® Foundation and Practitioner Certification
Job Linked
Study Abroad
Master of Business Administration (90 ECTS)Master of Business Administration (60 ECTS)Master in Computer Science (120 ECTS)Master in International Management (120 ECTS)Bachelor of Business Administration (180 ECTS)B.Sc. Computer Science (180 ECTS)MS in Data AnalyticsMS in Project ManagementMS in Information TechnologyMasters Degree in Data Analytics and VisualizationMasters Degree in Artificial IntelligenceMBS in Entrepreneurship and MarketingMSc in Data AnalyticsMS in Data AnalyticsMaster of Science in AccountancyMS in Computer ScienceMaster of Science in Business AnalyticsMaster of Business Administration MS in Data ScienceMS in Information TechnologyMaster of Business AdministrationMS in Applied Data ScienceMaster of Business AdministrationMS in Data AnalyticsM.Sc. Data Science (60 ECTS)Master of Business AdministrationMS in Information Technology and Administrative Management MS in Computer Science Master of Business Administration MBA General Management-90 ECTSMSc International Business ManagementMS Data Science MBA Business Technologies MBA Leading Business Transformation Master of Business Administration MSc Business Intelligence and Data ScienceMS Data Analytics MS in Management Information SystemsMSc International Business and ManagementMS Engineering ManagementMS in Machine Learning EngineeringMS in Engineering ManagementMSc Data EngineeringMSc Artificial Intelligence EngineeringMPS in InformaticsMPS in Applied Machine IntelligenceMS in Project ManagementMPS in AnalyticsMS in Project ManagementMS in Organizational LeadershipMPS in Analytics - NEU CanadaMBA with specializationMPS in Informatics - NEU Canada Master in Business AdministrationMS in Digital Marketing and MediaMS in Project ManagementMaster in Logistics and Supply Chain ManagementMSc Sustainable Tourism and Event ManagementMSc in Circular Economy and Sustainable InnovationMSc in Impact Finance and Fintech ManagementMS Computer ScienceMS in Applied StatisticsMS in Computer Information SystemsMBA in Technology, Innovation and EntrepreneurshipMSc Data Science with Work PlacementMSc Global Business Management with Work Placement MBA with Work PlacementMS in Robotics and Autonomous SystemsMS in Civil EngineeringMS in Internet of ThingsMSc International Logistics and Supply Chain ManagementMBA- Business InformaticsMSc International ManagementMS Computer Science with AIML ConcentrationMBA in Strategic Data Driven ManagementMaster of Business AdministrationMSc Digital MarketingMBA Business and MarketingMaster of Business AdministrationMSc Digital MarketingMSc in Sustainable Luxury and Creative IndustriesMSc in Sustainable Global Supply Chain ManagementMSc in International Corporate FinanceMSc Digital Business Analytics MSc in International HospitalityMSc Luxury and Innovation ManagementMaster of Business Administration-International Business ManagementMS in Computer EngineeringMS in Industrial and Systems EngineeringMSc International Business ManagementMaster in ManagementMSc MarketingMSc Business ManagementMSc Global Supply Chain ManagementMS in Information Systems and Technology with Business Intelligence and Analytics ConcentrationMSc Corporate FinanceMSc Data Analytics for BusinessMaster of Business AdministrationMaster of Business Administration 60 ECTSMaster of Business Administration 90 ECTSMaster of Business Administration 60 ECTSMaster of Business Administration 90 ECTSBachelors in International Management
For College Students

What is Exceptions in Java?


While writing your programs, you would have encountered many errors. It is indeed very irritating to encounter such errors. If a user is using your program and encounters such errors, he/she may lose trust in it. 


Let’s say you were loading a website page. The page didn’t load, but you got a simple message saying “Page not found”. Although there was an error, the website program was able to identify the error, mask the original error message, and send out a simple readable message — “Page not found”. But what if you got the entire technical message? You would lose trust in the creators of the website. Thus, you have to handle errors in a program. 


You may download the code used in the video from below:


Now, let’s look at some of the common exceptions that you see when you write your program and how to deal with them. 


Video Transcript


In the last lectures you learned about using inbuilt functions. In this lecture you will learn about handling errors in your program. Let's see what happens. When you run a program. The program is first compiled, then the program is run. That is, it is executed. However, when the program is running, it may run into problems or errors and stop execution. These problems are called exceptions. An exception, by definition occurs when the flow of a program is disrupted and ends abnormally. You don't want the program to end abnormally. You need it to tell you what error occurred. If any example exceptions may occur when the user enters invalid data or a file cannot be found. The exceptions may be caused by a user input or a flaw in the program logic. Let's look at an example. In here I have my data variable which is the result of 67 divided by zero. And then I simply want to print rest of the code goes here. Let's run this code over here. You can see that I just came across an exception. The exception in this thread reads arithmetic exception divided by zero. It states that there is some mathematical or arithmetic error and it is due to an attempted division by zero. Here you knew what the source of the error could be. The code following the source of the exception will not always be dependent on the source. It's not fair to stop the execution of the entire code just because there is an error. In a small independent part. The code should be able to identify what the error is and then move on to run the code following the error. Let's look at how to do this. Here we have made a small modification. Suppose that when the exception occurs, the compiler returns and prints the detail of the exception. Let's call the exception statement e. So the catch block catches the exception and prints it first. Let's execute this code to understand it better. As you can see, we are first printing out the exception, which you can see in the first line athematic exception divided by zero. And then we printed out the rest of the code is here. String, let's see what is happening here. At first, this code would try to divide 67 by zero. Now, since you can't divide by zero, the program would throw an Arseymatic exception. However, because you're using the try and catch block, the program will not crash like you saw earlier. Instead, it will actually execute this line. So the exception was stored in the variable E and it was printed in the print statement. Once it finishes executing the cache block, it will execute what comes after the try and catch block, which is system out print Ln. And you could see that the rest of the code is here statement was printed at the end. So to catch the error and not stop the code from executing altogether, you used a try catch feature in Java. This is how a try catch block would look like. You'd first have a try statement, and inside the parenthesis, you'd have your code statements that may cause exception followed by a catch block. A try block is always followed by a catch block. A try block contains a code where the exception may arise. If the exception occurs, it is caught by the catch block. The program doesn't stop altogether. Instead, the rest of the code is executed. In other words, the cache protects your program from ending abruptly. You may modify the code in the cache block to instead display a user friendly error message. This would help them to understand the exact error. So in here, instead of directly printing the exception, let's say I want to print a user friendly message saying error, do not divide number by zero and let's run this program again. As you can see, instead of printing the exception, the code simply printed out the user friendly message that we just typed. Trycatch helps you identify the part of the program where the error occurred. A catch block Catches Exception You can have multiple catch blocks that catch different exceptions. You can refer to the links below to learn more about multiple catches.


Video Recap


  • This segment is about handling errors in Java programming

  • When a program runs, it may encounter problems or errors called exceptions

  • Exceptions can be caused by invalid user input or flaws in program logic

  • It is important to catch exceptions and display user-friendly error messages instead of allowing the program to end abruptly

  • A try-catch block in Java can be used to catch exceptions and continue executing the code

  • The try block contains the code where the exception may occur and the catch block catches the exception and handles it

  • Multiple catch blocks can be used to catch different types of exceptions

  • It is possible to modify the catch block to display a user-friendly error message

  • The video provides an example of how to use a try-catch block in Java to handle an arithmetic exception caused by dividing by zero

Note: In JAVA, when you try to print 67/0, or store 67/0 into an int variable, Java throws an Arithmetic Exception. However, when you try to store 67/0 into a float or a double variable, it returns Infinity instead of an exception. 


You learnt how you can handle exceptions using the try and catch blocks. Here’s how a Try and Catch block looks:

try {
   // code that can throw exceptions 
} catch (Exception e) {

The try block contains the code where exceptions may arise. If an exception occurs, it is caught by the catch block, and the statement inside the block is executed. 


You can download the code used in the video from below :


Let us now learn more about different kinds of exceptions and how to deal with them so your program runs smoothly.   


A compile-time error is any type of error that prevents a java program from compiling, e.g. syntax error, incorrect data-types etc. Checked exceptions cause compile time errors if left unhandled.


A runtime error on the other hand arises while the program is running. This could happen due to unexpected user input or an incorrect logic etc.


So, now you know why you used “throws FileNotFoundException” when you were writing the code to read text from an external file. The throws keyword specifies that an exception may arise in the particular method. It is different from try and catch in that it has no provision to handle the exception and thus, it can’t catch the error and execute the corresponding action, e.g. displaying a different simplified message to the user 


In this lecture, you learnt about the various kinds of exceptions that may arise in your codes. You learnt about checked and unchecked exceptions. Checked exceptions are those that are checked while a program is being compiled. A program will not compile if there is no provision to handle these exceptions in your code. On the other hand, for unchecked exceptions, the program may compile even if there is no provision to handle them. But as a good practice, you should have a provision for handling these exceptions as well.