Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Help wanted] Is it possible to throw specified exception? #322

Open
yikuo123 opened this issue Nov 21, 2022 · 3 comments
Open

[Help wanted] Is it possible to throw specified exception? #322

yikuo123 opened this issue Nov 21, 2022 · 3 comments

Comments

@yikuo123
Copy link

FFmpegMediaSource.CreateFromUriAsync always throws unspecified error if there is something wrong:

System.Exception: Unspecified error (Exception from HRESULT: 0x80004005)

It might be HTTP 404/403 or something else and it is hard to handle these exceptions.

FFplay reports these detail errors, and I am wondering is it possible for FFmpegInteropX to throw specified errors like FFplay.

@lukasf
Copy link
Member

lukasf commented Nov 21, 2022

Unfortunately, the only kind of exceptions we can throw from C++/WinRT is HRESULT exceptions. We also cannot set any error messages or details. This leaves us with a very limited set of exception options. A few very common things like invalid argument are possible, but not nearly detailed enough to report the actual error situation.

We could declare our own HRESULT values. But I think it won't be very helpful, because you'd have to manually lookup which code means which error.

Also from ffmpeg side, it is often not easy to find out programmatically what has really gone wrong. Detailed error messages are often only output as log messages. You will find them in debug output of the app, if you are running in debugger. You could also register a log handler, to get the ffmpeg log output into an app's log file, when debugger is not attached (see FFmpegInteropLogging class).

@lukasf
Copy link
Member

lukasf commented Nov 21, 2022

Actually it looks like we can provide custom exception messages! I only never saw it, because the constructor overloads do not show up on intellisense (WTF)?!

This will show an exception with message "Test" in C# Sample:
throw winrt::hresult_error(E_FAIL, L"Test");

C++/CX also allowed to set custom messages, but they would never show on C# side, so I did not bother using them. Now that this seems to work in C++/WinRT, we could indeed be moving from HRESULT based model to throwing detailed exceptions, especially in the constructor methods. We can also use some of the HRESULT values from mferror.h (MF_E_*).

@yikuo123
Copy link
Author

Actually it looks like we can provide custom exception messages! I only never saw it, because the constructor overloads do not show up on intellisense (WTF)?!

This will show an exception with message "Test" in C# Sample: throw winrt::hresult_error(E_FAIL, L"Test");

C++/CX also allowed to set custom messages, but they would never show on C# side, so I did not bother using them. Now that this seems to work in C++/WinRT, we could indeed be moving from HRESULT based model to throwing detailed exceptions, especially in the constructor methods. We can also use some of the HRESULT values from mferror.h (MF_E_*).

It sounds great! Thank you for the detailed explanation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants