I am creating some classes that do not implement threading logic but need to be used off the main thread, so I want to throw NetworkOnMainThreadException (or a similar one for DB operations) in their constructors. How do I do that? How do Android do that, can I find it somewhere in the source code?
Asked
Active
Viewed 106 times
0
-
do you see this link : http://stackoverflow.com/questions/6343166/android-os-networkonmainthreadexception – Paul Jul 17 '15 at 07:24
-
This link is about how to avoid it, I dont want to avoid it but throw it – Kaloyan Roussev Jul 17 '15 at 07:30
-
oh sorry i don't understand your question, but i read that a network connection is impossible on main thread no ? – Paul Jul 17 '15 at 07:32
-
@Paul he wants to throw that exception if the runs on the UI Thread. – Blackbelt Jul 17 '15 at 07:52
1 Answers
2
you could check the current's thread looper. E.g.
if (Looper.myLooper() == Looper.getMainLooper()) {
throw new NetworkOnMainThreadException();
}
I would avoid it anyway. If I were in you I would document the method/class to clarify it has to be executed on an asynchronously

Blackbelt
- 156,034
- 29
- 297
- 305
-
I used to name these classes by appending "Synchronous" at the end of the name but I think it's ugly. – Kaloyan Roussev Jul 17 '15 at 07:34
-