Exception Handling C Tutorial

👁 1 مشاهدة

Exception Handling C Tutorial

النص الكامل للفيديو

in this video we're going to learn about exceptions in plus exceptions are feature built into plus for handling unexpected or unwanted events that occur during our program's execution so code in our program can throw an exception to indicate that something has gone wrong this will normally cause our program to crash with an error message unless we catch the exception using what's called try catch block allowing us to handle the exception in some way without the program crashing exceptions can be thrown by some of the built-in plus operators and standard Library functions so for example we can declare string variable called word and we'll set it to the word for we can then access the individual characters of this string using the at method suicide year out and then word dot at 3 followed by an inline so at three is going to give us the fourth character in the string which is going to be lowercase we can save compile and run the program and we get lowercase what if though we had dot at four there is no fifth character in the string if we save compile and run the program now we'll get this terminating with uncaught exception of type STD colon colon out of range basic string so our program has crashed with an error this is an uncut exception so what's happened is the at method has thrown an exception but we never caught the exception let's actually try to catch the exception Now using try catch block we're going to wrap this code in try catch block so we put the code that may throw an exception inside the tri-block portion of the try catch block the try block is created with try and then open curly brace and then closed curly brace in the try block is going to allow us to essentially listen for an exception to potentially catch an exception when one occurs to catch the exception we also need to use catch block so here we'll have catch and we'll have dot dot dot and then another block and this is catch block now if an exception is thrown in this try block execution is going to jump to this catch block where we can have code to handle that exception so for example we could output here exception throne followed by an inline if we save compile and run the program this time we no longer have the program crash instead we output exception throne so what happened is when this code here through the exception we caught it and we output exception Throne followed by an end line so exceptions give us mechanism for recognizing when unexpected program Behavior has occurred and for handling it now exceptions are thrown with type this catch block here is special catch block that's going to catch any type of exception the exception thrown here is going to be an out of range exception we could have here out of range and then and and this catch block will catch this specific type of exception now when the exception is thrown and out of range type object is going to be provided is going to be reference to that object is bit like function parameter that's going to be set to the object thrown in the try block now is an object that has method called what any what method is going to return string which may contain information about the exception that has taken place so here we could have exception colon and then we'll have dot what to Output that string and if we save compiled and run the program now we get exception colon basic underscore string so that's what the what method must have returned now this catch block is only going to catch exceptions of the type out of range but there are other types of exceptions so for example let's try to dynamically allocate space for an array of ins that's way too large we'll have here int star array is equal to new int and we'll have an array with length that's way too large we'll save compile and run the program and now we get this different exception here it says terminating with uncut exception of type bad underscore alloc so this is different type of exception and because of that this catch block here is not going to catch it so we could actually have second catch block to catch this type of exception we could have here catch and then bad underscore Alec and and here we'll output second catch colon followed by dot What followed by an inline and we'll change this one here to First Catch just so we can tell the difference now this bad Alec exception should be caught by the second catch block we'll try this out we'll save compile and run the program and now we get second catch and STD colon colon bad underscore Alec so now the second catch block is catching the exception of that type no NC plus plus there's hierarchy of exception types so at the top there is base exception class and bad Alec is derived class of exception out of range is drive class of logic error which is drive class of exception now of course the entire hierarchy is more complicated than this I'll post link in the video description covering that hierarchy but because bad Alec is derived class of exception it is also an exception so here if we had exception instead of bad Alec this will also catch bad Outlook exception we could save compile and run the program and again we'll get second catch and STD colon colon bad Alec so we need to be mindful of this when creating our catch blocks because our catch blocks are going to be checked in order so for example if here we had bad Alec and then up here instead of out of range we had exception even though we're trying to specifically catch bad Alec exception we'll never reach this code because the first catch is going to match that bad Outlook exception we could save compile and run the program and we'll get here first catch so we need to be mindful of this when creating our catch blocks so for example we could first try to catch and handle specific types of exceptions then afterwards we could try to catch and handle the general exception type we could have here catch and then exception and and we could have here out exception caught colon followed by dot what followed by an end line and so this catch block is going to match for any type of exception object being thrown we can actually throw our own exceptions so up here let's actually comment this out and we'll throw our own exception we'll have here throw and then exception and if we save compile and run the program we'll get here exception caught and here we're using the exception type so it's that last catch block that matches we could throw specific type of exception so for example we could have here throw and we'll have runtime error and why should you give message here now too we'll have problem encountered and then we'll save compile and run the program and now we get exception Cod problem encountered we can actually create our own custom exception types we can then throw and catch exceptions of those types for example we can create drive class of exception so up here we'll do that we'll have here class and then custom exception and we'll make it derived class of exception and we'll provide our own definition of the what method we'll have virtual const car pointer what and then const no accept and we'll have the function return the string custom exception then down here we'll throw this custom exception we'll have throw and then custom exception so delete this and we'll have throw custom exception and if we save compile and run the program we see that our custom exception was thrown and caught one day I'll make whole video going over creating our own custom exception types but one thing should mention is this no accept specifier this specifier means that this function will not throw an exception and that makes sense because this function is helping to Define and accept and type we wouldn't want the function to also throw an exception now we don't actually have to throw an exception type object we can throw any type of value and catch it too so for example we could have here throw and we'll throw let's say 20. if we save compile and run the program right now we're not going to actually catch this exception we get terminating with uncut exception of type end and that's because none of our catch blocks are looking for an in-type exception we could actually catch an in-type exception so down here we'll add new catch block we'll have catch and then int code and the int that we throw as an exception is going to be what code is set to we could output here code colon and we'll put the code followed by an end line and now if we save compile and run the program we'll get error code 20 and we have now caught that exception so if we want to have true Last Chance default catch case that will catch anything thrown we should have here catch and then dot dot dot and then we'll have in here out default catch case followed by an inline now up here if we try to throw something that is not an INT or an exception type object we can still catch it so if we throw double type value like 5.6 we can now catch this exception we can save compile and run the program it will get here default catch case now we can actually throw the exception inside function that we call so for example we could have here void my function1 and this function is going to throw the exception we'll have throw 5.6 then down here if we call the function with my function 1 we'll catch the exception that this function throws so we can save compile and run the program and again we'll catch the exception it doesn't really matter how deep the exception occurs in series of function calls when an exception is thrown we can sort of think of the exception as floating up through the series of function calls until the closest catch block is able to catch it so for example if my function 1 called my function 2 and my function 2 throws the exception so we could have void my function two and throw 5.6 even though the exception is happening inside function call inside another function call the exception is going to propagate upwards until it hits our catch block so we could save compile and run the program and we'll still catch the exception with default catch case so there's few more things that want to mention about exceptions in general as good practice we want to catch and handle specific types of exceptions or at least exception types that are more specific than less specific the idea is that we should know what types of problems could occur and how to handle those problems and if we're relying on catch-all cases to handle problems then maybe we don't know what's really going on now I'm not saying never to use catch-all catch blocks but you should be careful that you're not using them as way to mask unknown errors that are occurring now that we know the mechanics of exceptions should mention that big reason why we use exceptions is that they allow us to separate code that handles errors from the algorithms in which those errors could occur we don't need to clutter up our functions trying to do real work with error handling work we can let them focus on solving problem and we can let our exception handling code deal with the errors separating concerns like this is generally good thing in software development it is possible to misuse exceptions the way that exceptions jump control flow immediately to catch block might make attempting to use them to actually Implement control flow as part of some algorithm that will create but this would make code that's difficult to trace and follow in similar way that using go to statements makes code difficult to trace and follow so this is how we can use exceptions in plus checkout portfoliocourses.com where we'll help you build portfolio that will impress employers
Intro to Computer Science Module 09 Exception Handling for Run Time Errors 17:52

Intro to Computer Science Module 09 Exception Handling for Run Time Errors

Access 2 Learn

146 مشاهدة · 3 years ago

How To Debugging Stack Trace Exception Handling Try Except Raise 21:14

How To Debugging Stack Trace Exception Handling Try Except Raise

Mnemonic Academy

490 مشاهدة · 9 years ago

JAVA Exceptions Exercise 2 5:05

JAVA Exceptions Exercise 2

ProCodeMaster

108 مشاهدة · 4 years ago

Java Exception Handling Tutorial 23:03

Java Exception Handling Tutorial

Programming with Mosh

242.2K مشاهدة · 4 years ago

What is Exception in Java 5:19

What is Exception in Java

Telusko

239.3K مشاهدة · 3 years ago

Exception handling in C How to handle errors in your program 24:46

Exception handling in C How to handle errors in your program

CodeBeauty

126.3K مشاهدة · 5 years ago

JAVA Exceptions Exercise 1 4:49

JAVA Exceptions Exercise 1

ProCodeMaster

383 مشاهدة · 4 years ago

Java Exception Handling Demystified Master Error Gracefully in Java Intermediate Tutorial 7:25

Java Exception Handling Demystified Master Error Gracefully in Java Intermediate Tutorial

The Code Compiler

1 month ago

11 13 Exception Handling Java Foundations Certification 2:03:26

11 13 Exception Handling Java Foundations Certification

Code with Nakov | SoftUni Global

881 مشاهدة · 4 years ago

What Disrupts Your Code Error vs Exception 0:07

What Disrupts Your Code Error vs Exception

innovate hubtec

2 مشاهدة · 6 days ago

B 2 1 3 Describe how programs use common exception handling techniques 8:48

B 2 1 3 Describe how programs use common exception handling techniques

Mr. Rhed CS

4 مشاهدة · 8 days ago