0Day Forums
Where to catch exceptions in a MVC framework - Printable Version

+- 0Day Forums (https://0day.red)
+-- Forum: Coding (https://0day.red/Forum-Coding)
+--- Forum: FrameWork (https://0day.red/Forum-FrameWork)
+--- Thread: Where to catch exceptions in a MVC framework (/Thread-Where-to-catch-exceptions-in-a-MVC-framework)



Where to catch exceptions in a MVC framework - spas426982 - 07-20-2023

Everyone talks about how important exception handling is. I have never had the need to handle them until recently:

try {
$pdo = new PDO($dns);
} catch (Exception $e) {
throw new Exception($e);
}

Another more general example would be:

if ($something) {
throw new Exception('Ouch! I can't do that!');
}

After a little online research, I found that many examples/solutions online simply echo them. (

[To see links please register here]

) Some don't have solutions. (

[To see links please register here]

) & (

[To see links please register here]

) And others, I just dont understand. (

[To see links please register here]

)

**Question #1:** Where should I catch exceptions; in the Model or Controller of a **MVC**ish framework?

**Question #2:** What should catching it do? Log it, email it, display 503?


RE: Where to catch exceptions in a MVC framework - jackinthebox816 - 07-20-2023

I am a .net guy and for me best practices for catching exceptions:

- Catch it as late as possible.
- Write a common code to catch exception.
- Catch it at controller level and log it somewhere with complete stack trace (ETW, Log file, DB etc.).
- Redirect user to some good looking error page. Only controller can do this.