Laravel Exceptions: Why and How to Use? Practical Example.

  Рет қаралды 6,162

Laravel Daily

Laravel Daily

Күн бұрын

Пікірлер: 34
@王肖毅-m4t
@王肖毅-m4t 11 ай бұрын
It is very useful to throw different exceptions and handle them differently in the project.
@amilsonm
@amilsonm 5 ай бұрын
Friend, is there any way to add balance to multiple users' wallets at once?
@jamzykimani
@jamzykimani 11 ай бұрын
The video and audio is okay. Just a small comment, I noticed the alert message for insufficient balance was green instead of red/orange.
@Krishnan172
@Krishnan172 11 ай бұрын
Sir how do you come up with all new & different topics so frequently on single thing i.e. Laravel. And too for many years. No doubt you have immersed yourself into this for consistent periods, thus it is helping developers like us even today. But how do you do it?
@LaravelDaily
@LaravelDaily 11 ай бұрын
I spend 30-60 minutes every day reading Laravel conversations on Twitter, Laracasts forum, Stackoverflow, KZbin. Also, I get a lot of questions from my audience. That way, my pile of ideas is getting only bigger every day.
@JJ15ASMR
@JJ15ASMR 11 ай бұрын
Great example, I like using custom exceptions the same way (however, I tend to abstract the message to a more user-friendly one when catching since some exceptions might be more targeted at the developer, e.g. API request fails). The only thing I noticed is that your facecam is a bit smaller than before, but I don't think it really matters.
@LaravelDaily
@LaravelDaily 11 ай бұрын
Yeah small cam is fine because the videos are not about me :)
@wowdesigns7436
@wowdesigns7436 11 ай бұрын
There is a problem .. the resolution is very high .. keep it a little low
@obikwelukyrian6848
@obikwelukyrian6848 11 ай бұрын
For me, custom exceptions make more sense in packages, when you structure your application in a modular way where every module is seperate. But for a regular laravel app, I don't see why I need to. Yet
@hentype
@hentype 11 ай бұрын
You'd want to use exception for situations where a logic block can fail in multiple ways and want to just catch them altogether. It's an efficient way to deal with those types of errors. Imagine you're using something like a task with an api call to an external service that might not be available at rare times which is out of your control. You might want to catch that as well then push the task for a later retry queue or even skip the current entry next time instead of risking the occasional fatal error and ruining the process.
@gazorbpazorbian
@gazorbpazorbian 11 ай бұрын
great video! I love it, could you make a more in depth video on exceptions and how to use them in your project? I find it confusing when you can simply send an error message without the use of an exception, for example the response could have been return response()->json(["message" => "this is an error"],500)
@LaravelDaily
@LaravelDaily 11 ай бұрын
We have a full course on Handling Errors and Exceptions in Laravel: laraveldaily.com/course/exceptions-errors-laravel
@gryg666
@gryg666 11 ай бұрын
What are the benefits of putting the exception message as a constructor parameter and using parent constructor over just replacing protected property $message?
@AlexGower
@AlexGower 11 ай бұрын
I assume because if you want to manually trigger the exception somewhere else with a custom message.
@gryg666
@gryg666 11 ай бұрын
@@AlexGower You can still do that using constructor from the Exception class.
@LaravelDaily
@LaravelDaily 11 ай бұрын
Good question. Not sure, to be honest, maybe personal preference.
@logudotcom
@logudotcom 11 ай бұрын
Loved it...
@al.e.k
@al.e.k 11 ай бұрын
Hello! By the way, all exceptions in this package extend just Exception. But I suppose it would be better to extend from common package-specific exception, e.g. PayPocketException. And then it would be easier to catch package specific range of exceptions, not one by one. In your example you catch only InsufficientBalanceException, but after it you could catch common PayPocketException to make pay flow more reliable. What do you think about it?
@LaravelDaily
@LaravelDaily 11 ай бұрын
Maybe that could be the next step, yes.
@allforone772
@allforone772 11 ай бұрын
Insufficiency balance is an expected case and can occur many times... in my opinion, it's not at all an exception, just a general workflow.
@Krishnan172
@Krishnan172 11 ай бұрын
Sir will you be available for direct virtual connect on google meet or so? I promise I won't take much of your time.
@LaravelDaily
@LaravelDaily 11 ай бұрын
No sorry I don't do personal consulting anymore, just don't have time
@binaryfire
@binaryfire 11 ай бұрын
Personally I don't think it's a good idea to use exceptions for things that are a normal part of the app's functionality (like "insufficient balance"). Better to handle that with app logic. IMHO exceptions should be reserved for unexpected errors that occur in the code. I.e. things that the developers should be alerted about.
@genechristiansomoza4931
@genechristiansomoza4931 11 ай бұрын
Exceptions are normal part of app's functionality
@binaryfire
@binaryfire 11 ай бұрын
​@@genechristiansomoza4931 From the Oracle Java docs: "Definition: An exception is an event, which occurs during the execution of a program, that disrupts the normal flow of the program's instructions. When an error occurs within a method, the method creates an object and hands it off to the runtime system. The object, called an exception object, contains information about the error, including its type and the state of the program when the error occurred. Creating an exception object and handing it to the runtime system is called throwing an exception." You can use exceptions however you like, but the idea behind them is they're thrown when an * error * occurs. Using them as shortcut to display error messages to users isn't a great idea. If you start exposing some exception messages to users, there's a higher chance of accidentally exposing others that contain sensitive information. It's better to just use exceptions for recording and report errors via trackers like Sentry / Bugsnag etc. And handle checking for things like insufficient balances using normal code logic. But that's just my opinion. Each to their own.
@binaryfire
@binaryfire 11 ай бұрын
@@genechristiansomoza4931 I posted a long reply but it disappeared for some reason. Short answer is no, exceptions are usually thrown in response to errors. Not in response to negative conditions in the normal control flow. That's a universally accepted concept in every programming language. A simple if statement to return the "insufficient funds" message if the balance is below the required amount would be better than treating it as an error.
@shepard153
@shepard153 11 ай бұрын
​@@binaryfire I agree. Looks like over engineering to me. Exceptions are used when something unexpected happens, like db transaction fails where it shouldn't. Hence the name - exception.
@patrickgomes2261
@patrickgomes2261 11 ай бұрын
If you are developing a package I would always prefer the exception option like the package shown in the video, its way cleaner in the code to do whatever you want to do, rather then returning a false or true or a array if data, a string or whatever, that you would need to check within a if or switch statement. And no, exception should not only be thrown when some error occurs, for me everything the client is trying to do should be always a success and if not then its an exception, lets say the user tries to pay for something he should be able to but if the card has been declined from the bank then its an exception and many payment gateways follow this. Edit: I am not anyone should throw exception for everything, rather saying its ok to use exception beyond error syntax and stuff like that, especially if you are creating a package or something like this that will be use for other developers, like an api or so.
@milan1337
@milan1337 11 ай бұрын
@LaravelDaily As you requested at the start of this video, the audio in this video is way more "roomy" than normal, perhaps due to your new MacBook or maybe the room. Anyway thank you for your videos!
@LaravelDaily
@LaravelDaily 11 ай бұрын
That's because of new office room, indeed. But this has been roomy for a week already, that's not about the macbook :) I'm lowering it a bit with Noise Removal
@milan1337
@milan1337 11 ай бұрын
@@LaravelDaily oh sorry I must have missed the other videos in the new office. Good luck!
@shahsawoodshinwari
@shahsawoodshinwari 11 ай бұрын
Actually, in my real-time projects, I didn't find exceptions helpful or meaningful, so have left using them
@rutgerhoutdijk3547
@rutgerhoutdijk3547 11 ай бұрын
You don't want code that fails silently, exceptions make everything more readable and easy to debug.. You just need to get used to throwing exceptions in your code instead of returning null/false/etc.. 😅
Exceptions in Laravel: Why/How to Use and Create Your Own
12:18
Laravel Daily
Рет қаралды 89 М.
Laravel API Code Review: Inconsistent Code May Cost You a Job
10:47
Laravel Daily
Рет қаралды 4,9 М.
小丑女COCO的审判。#天使 #小丑 #超人不会飞
00:53
超人不会飞
Рет қаралды 16 МЛН
Chain Game Strong ⛓️
00:21
Anwar Jibawi
Рет қаралды 41 МЛН
So Cute 🥰 who is better?
00:15
dednahype
Рет қаралды 19 МЛН
The evil clown plays a prank on the angel
00:39
超人夫妇
Рет қаралды 53 МЛН
What Senior Devs ACTUALLY Do? (And how to become one)
7:12
Laravel Daily
Рет қаралды 8 М.
Top 5 Laravel "Bad Practices" (My Opinion)
10:32
Laravel Daily
Рет қаралды 25 М.
PowerShell Universal + dbatools!
13:57
Jess Pomfret
Рет қаралды 385
Разбор Error Handling в Laravel. Под капотом Laravel
25:50
Просто о сложном. CutCode
Рет қаралды 2,6 М.
100 Days of One Punch Man Workout | Transformation Results
10:13
100 Days
Рет қаралды 15 МЛН
Laravel Controller Code: Move to Model, Service, Action or Job?
12:51
Laravel Daily
Рет қаралды 114 М.
Exceptions в PHP. Урок с курса продвинутых методик Laravel от CutCode
23:33
Просто о сложном. CutCode
Рет қаралды 2,4 М.
Two Things Laravel Services Should NOT Do
8:20
Laravel Daily
Рет қаралды 22 М.
Laravel: Avoid Try-Catch In Every Method (What To Do Instead)
4:45
Laravel Daily
Рет қаралды 37 М.
Laravel Code from Controller into Service: The Right Way
5:30
Laravel Daily
Рет қаралды 63 М.
小丑女COCO的审判。#天使 #小丑 #超人不会飞
00:53
超人不会飞
Рет қаралды 16 МЛН