+ 2

try and catch in php

So if i have a code like try{ randomfunction(); }catch(){} And the random function tries to create a new file in local What will happen if the function fails will an exception be thrown or just the process fails and no exception is thrown from the catch.

5th May 2022, 10:56 AM
Rohan Raj Shrestha
Rohan Raj Shrestha - avatar
3 Respuestas
+ 3
I'm not familiar with php but I'm pretty sure that try catch works the same in all languages. Without try catch block -> exception will be thrown With try catch block -> The exception will be catched and you need to define in your catch function what should happen. In general: try part fails -> catch part is executed For example a user want to open a file which does not exist. You don't want to crash your program so you catch the exception and maybe print a error message ("file not found") and the user can try to open another file or leave it and do something else with your program.
5th May 2022, 4:15 PM
Denise Roßberg
Denise Roßberg - avatar
+ 1
I think so. Maybe this tutorial helps you: https://netgen.io/blog/modern-error-handling-in-php
6th May 2022, 2:51 PM
Denise Roßberg
Denise Roßberg - avatar
0
Will it catch the error even if it is caused inside a function that is allocated in a different class but called inside the try-catch block in a separate class?
6th May 2022, 8:14 AM
Rohan Raj Shrestha
Rohan Raj Shrestha - avatar