Rons
Rons

Reputation: 11

Laravel custom exception no directory

I am using laravel Local Filesystem.

how to throw custom exception if there no directory when using file system. I want make new message like "no Folder found" or any custom

mkdir(): No such file or directory

I want to create custom exception when running Storage::disk('local')->Files('/no folder/);

or when setting custom disk 'root' => "D:/No Folder/",

Upvotes: 0

Views: 1116

Answers (1)

OskarD90
OskarD90

Reputation: 633

Here is a guide on creating your own PHP exception with the Laravel framework:

  1. Create a new file inside your app/Exceptions directory called MyCustomException.php
  2. Put the PHP-tag on the top of the file: <?php
  3. Assign the file to the namespace App\Exceptions: namespace App\Exceptions;
  4. Create a class inside with the same name as the file (Without ".php"), extending the native Exception class: class MyCustomException extends \Exception { }
  5. In your code where you want to throw the exception, use: throw new App\Exceptions\MyCustomException('My own message here');

You can read more about exceptions here, and about extending exceptions here. If you want to extend an existing, built-in, exception; here is a useful page for finding which one you want to extend.

If you just want to throw a regular exception with your own message, just use throw new \Exception('My own message here');

Upvotes: 0

Related Questions