Julia Tutorial - 10. Exception Handling
Skills:
Algorithm Basics70%
Key Takeaways
Exception handling in Julia using try-catch blocks
Full Transcript
hello everybody let's talk about exceptions in Julia programming language today exceptions are errors detected at the time of program execution for example if you try to concatenate string with a number using plus operator it will give you this error this is a load error exception another one is you're trying to find out the square root of a negative number and you know that's not defined so it will give you this domain error so this is another the exceptions so if you go to uh Julia l.org website uh it has a list of all these exceptions okay so we just looked at domain error the problem with this exceptions is that if you are running uh your code and if this accept H happens it will terminate the execution of the code so sometimes this might be disastrous you what you want to do instead is whenever acception occurs you want to handle it and gracefully uh take like a detour you know let's say you are driving on a road and you find an accident you don't terminate driving right right you take a detour so exception handling is sort of like taking a detour and finding an alternate path uh in your program execution so here I'm going to show you how you can use TR catch to uh handle this square root exception for that you can write try oops I hit alter enter by mistake uh sqrd catch exception occur so the code that you suspect might throw an exception you have to enclose that into this try catch block and here in catch you'll write a code that you want to execute upon uh exception occurrence so this is short of like a you're taking a D2 and this is n so due to this what will happen is your program will not terminate it will continue execution even Beyond this point okay so if I execute this it will say exception occurred okay now here what I did is I handled all kind of exceptions into this generic block sometimes you may want to know the type of exception and for that you can specify your exception object here so when you say catch y your exception is stored in this y object and you can print that here so if I hit alter enter it says domain error so the type of exception that occurred was a domain error okay now sometimes you don't know what kind of exception will happen so you want to make a check on it and what I'm going to do here is I will throw two kind of exceptions so here I have an array and what I'm doing here is finding the square root of second element of this array okay now possible exception that may happen here is domain error if any of these elements is less than minus one if this is minus one then it will result into a domain error but let's say if I do something like index array out of bound then it will throw me this error bounds error right so uh if you want to do a specific check on type of exception you can use is a function you can say if is a your exception object domain error if the type of exception that is thrown is domain error then you can just say domain error domain error else if the type of exception is bounce error then you will say okay this one is Bounce error okay and I forgot and so let's execute this so now it is saying bounce error based on this type check if I do two and try to find square root ofus this one it will go through this one so now it hit this one previously it was hitting this line so this this is how you can check the type of exception and based on the time you can do necessary handling I'm just using print allent right now but but in the real world code you would have you will have a real code uh return to handle the type of exception okay now sometimes uh uh you may want to throw your own exception uh you probably don't want to use this system generated exception in that case you can use a throw statement so here let's say if I have this kind of code I'm just throwing the exception straight away but but you might have some custom function return or custom logic where certain conditions cannot be handled in which you will throw uh exception so for this you will use a throw statement and in the bracket you can uh specify what type of exception you want to throw so here I'm using domain error again we looked at Julia website and we have all these exceptions so you can throw any of these or you can have generic exception object as well again when I execute this it is saying domain error okay so that was a sweet uh like short and sweet introduction on uh exception thanks for watching
Original Description
Do you want to learn technology from me? Check https://codebasics.io/ for my affordable video courses.
Website: http://codebasics.io/
Facebook: https://www.facebook.com/codebasicshub
Twitter: https://twitter.com/codebasicshub
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from codebasics · codebasics · 31 of 60
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
▶
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Python Tutorial - 1. Install python on windows
codebasics
Python Tutorial - 2. Variables
codebasics
Python Tutorial - 3. Numbers
codebasics
Python Tutorial - 4. Strings
codebasics
Python Tutorial - 5. Lists
codebasics
Python Tutorial - 6. Install PyCharm on Windows
codebasics
PyCharm Tutorial - 7. Debug python code using PyCharm
codebasics
Python Tutorial - 8. If Statement
codebasics
Python Tutorial - 9. For loop
codebasics
Python Tutorial - 10. Functions
codebasics
Python Tutorial - 11. Dictionaries and Tuples
codebasics
Python Tutorial - 12. Modules
codebasics
Python Tutorial - 13. Reading/Writing Files
codebasics
How to install Julia on Windows
codebasics
Python Tutorial - 14. Working With JSON
codebasics
Julia Tutorial - 1. Variables
codebasics
Julia Tutorial - 2. Numbers
codebasics
Python Tutorial - 15. if __name__ == "__main__"
codebasics
Julia Tutorial - Why Should I Learn Julia Programming Language
codebasics
Python Tutorial - 16. Exception Handling
codebasics
Julia Tutorial - 3. Complex and Rational Numbers
codebasics
Julia Tutorial - 4. Strings
codebasics
Python Tutorial - 17. Class and Objects
codebasics
Julia Tutorial - 5. Functions
codebasics
Julia Tutorial - 6. If Statement and Ternary Operator
codebasics
Julia Tutorial - 7. For While Loop
codebasics
Python Tutorial - 18. Inheritance
codebasics
Julia Tutorial - 8. begin and (;) Compound Expressions
codebasics
Python Tutorial - 12.1 - Install Python Module (using pip)
codebasics
Julia Tutorial - 9. Tasks (a.k.a. Generators or Coroutines)
codebasics
Julia Tutorial - 10. Exception Handling
codebasics
Python Tutorial - 19. Multiple Inheritance
codebasics
Python Tutorial - 20. Raise Exception And Finally
codebasics
Python Tutorial - 21. Iterators
codebasics
Python Tutorial - 22. Generators
codebasics
Python Tutorial - 23. List Set Dict Comprehensions
codebasics
Python Tutorial - 24. Sets and Frozen Sets
codebasics
Python Tutorial - 25. Command line argument processing using argparse
codebasics
Debugging Tips - What is bug and debugging?
codebasics
Debugging Tips - Conditional Breakpoint
codebasics
Debugging Tips - Watches and Call Stack
codebasics
Python Tutorial - 26. Multithreading - Introduction
codebasics
Git Tutorial 3: How To Install Git
codebasics
Git Tutorial 1: What is git / What is version control system?
codebasics
Git Tutorial 2 : What is Github? | github tutorial
codebasics
Git Tutorial 4: Basic Commands: add, commit, push
codebasics
Git Tutorial 5: Undoing/Reverting/Resetting code changes
codebasics
Git Tutorial 6: Branches (Create, Merge, Delete a branch)
codebasics
Git Github Tutorial 10: What is Pull Request?
codebasics
Git Tutorial 7: What is HEAD?
codebasics
Git Tutorial 9: Diff and Merge using meld
codebasics
Difference between Multiprocessing and Multithreading
codebasics
Python Tutorial - 27. Multiprocessing Introduction
codebasics
Python Tutorial - 28. Sharing Data Between Processes Using Array and Value
codebasics
Git Tutorial 8 - .gitignore file
codebasics
Python Tutorial - 29. Sharing Data Between Processes Using Multiprocessing Queue
codebasics
Python Tutorial - 30. Multiprocessing Lock
codebasics
Python Tutorial - 31. Multiprocessing Pool (Map Reduce)
codebasics
What is code?
codebasics
Python unit testing - pytest introduction
codebasics
More on: Algorithm Basics
View skill →
🎓
Tutor Explanation
DeepCamp AI