1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
use crate::binds::MonoObject;
use crate::gc::{gc_unsafe_enter, gc_unsafe_exit, GCHandle};
use crate::{Class, Domain, Image, InteropClass, ObjectTrait};
use std::ffi::CString;
/// Safe representation of `MonoException`.
pub struct Exception {
    #[cfg(not(feature = "referneced_objects"))]
    exc_ptr: *mut crate::binds::MonoException,
    #[cfg(feature = "referneced_objects")]
    handle: GCHandle,
}
impl Exception {
    /// Raise exception (it can be then cached by catch clause in managed code)
    /// # Safety
    /// This function is extremely unsafe, because when it is called, drop functions of local variables **are not** automatically  called.
    /// # Example
    /// ## C#
    ///```csharp
    /// using System.Runtime.CompilerServices;
    /// class SomeClass{
    ///     [MethodImplAttribute(MethodImplOptions.InternalCall)]   
    ///     void ExceptionThrower();
    ///     void SomeMethod(){
    ///         try{
    ///             ExceptionThrower();
    ///         }
    ///         catch(exception){
    ///             Console.WriteLine("This will always catch exceptions raised in ExceptionThrower");
    ///         }
    ///     }
    /// }
    ///```
    /// ## Rust
    ///```rust
    /// # use wrapped_mono::{invokable,Exception};
    /// #[invokable]
    /// fn exception_thrower(){
    ///     let some_local_data = vec![12,2,35,32];
    ///     let exception = Exception::not_implemented("This function will just throw exceptions!");
    ///     // Everything needs to be dropped before exception is thrown!
    ///     drop(some_local_data);
    ///     unsafe{exception.raise()};
    /// }
    #[allow(clippy::missing_panics_doc)] // This may never panic, because it is impossible to return from `raise`
    pub unsafe fn raise(&self) -> ! {
        unsafe { crate::binds::mono_raise_exception(self.get_ptr().cast()) };
        panic!("After an exception is thrown, nothing should happen.");
    }
    /// Creates [`Exception`] of type *name* in *namespace* from *image* in *domain*'
    #[must_use]
    pub fn from_name_domain(
        domain: &Domain,
        image: Image,
        namespace: &str,
        name: &str,
    ) -> Option<Self> {
        let namespace_cstr = CString::new(namespace).expect(crate::STR2CSTR_ERR);
        let name_cstr = CString::new(name).expect(crate::STR2CSTR_ERR);
        #[cfg(feature = "referneced_objects")]
        let marker = gc_unsafe_enter();
        let res = unsafe {
            Self::from_ptr(
                crate::binds::mono_exception_from_name_domain(
                    domain.get_ptr(),
                    image.get_ptr(),
                    namespace_cstr.as_ptr(),
                    name_cstr.as_ptr(),
                )
                .cast(),
            )
        };
        #[cfg(feature = "referneced_objects")]
        gc_unsafe_exit(marker);
        let _ = namespace_cstr;
        let _ = name_cstr;
        res
    }
    /// Creates [`Exception`] of type *name* in *namespace* from *image*
    #[must_use]
    pub fn from_name(image: Image, namespace: &str, name: &str) -> Option<Self> {
        let namespace_cstr = CString::new(namespace).expect(crate::STR2CSTR_ERR);
        let name_cstr = CString::new(name).expect(crate::STR2CSTR_ERR);
        #[cfg(feature = "referneced_objects")]
        let marker = gc_unsafe_enter();
        let res = unsafe {
            Self::from_ptr(
                crate::binds::mono_exception_from_name(
                    image.get_ptr(),
                    namespace_cstr.as_ptr(),
                    name_cstr.as_ptr(),
                )
                .cast(),
            )
        };
        #[cfg(feature = "referneced_objects")]
        gc_unsafe_exit(marker);
        let _ = namespace_cstr;
        let _ = name_cstr;
        res
    }
    /// Creates [`Exception`] of type *name* in *namespace* from *image* with message *msg*.
    #[must_use]
    pub fn from_name_msg(image: Image, namespace: &str, name: &str, msg: &str) -> Option<Self> {
        let namespace_cstr = CString::new(namespace).expect(crate::STR2CSTR_ERR);
        let name_cstr = CString::new(name).expect(crate::STR2CSTR_ERR);
        let msg_cstr = CString::new(msg).expect(crate::STR2CSTR_ERR);
        #[cfg(feature = "referneced_objects")]
        let marker = gc_unsafe_enter();
        let res = unsafe {
            Self::from_ptr(
                crate::binds::mono_exception_from_name_msg(
                    image.get_ptr(),
                    namespace_cstr.as_ptr(),
                    name_cstr.as_ptr(),
                    msg_cstr.as_ptr(),
                )
                .cast(),
            )
        };
        #[cfg(feature = "referneced_objects")]
        gc_unsafe_exit(marker);
        let _ = msg_cstr;
        let _ = namespace_cstr;
        let _ = name_cstr;
        res
    }
    /// Returns [`Exception`] that is instance of **`System.ArgumentException`**
    #[must_use]
    pub fn argument_exception(arg: &str, msg: &str) -> Self {
        let arg_cstr = CString::new(arg).expect(crate::STR2CSTR_ERR);
        let msg_cstr = CString::new(msg).expect(crate::STR2CSTR_ERR);
        #[cfg(feature = "referneced_objects")]
        let marker = gc_unsafe_enter();
        let res = unsafe {
            Self::from_ptr_unchecked(
                crate::binds::mono_get_exception_argument(arg_cstr.as_ptr(), msg_cstr.as_ptr())
                    .cast(),
            )
        };
        #[cfg(feature = "referneced_objects")]
        gc_unsafe_exit(marker);
        let _ = arg_cstr;
        let _ = msg_cstr;
        res
    }
    /// Returns [`Exception`] that is instance of **`System.NotImplementedException`**
    #[must_use]
    pub fn not_implemented(msg: &str) -> Self {
        let msg_cstr = CString::new(msg).expect(crate::STR2CSTR_ERR);
        #[cfg(feature = "referneced_objects")]
        let marker = gc_unsafe_enter();
        let res = unsafe {
            Self::from_ptr_unchecked(
                crate::binds::mono_get_exception_not_implemented(msg_cstr.as_ptr()).cast(),
            )
        };
        #[cfg(feature = "referneced_objects")]
        gc_unsafe_exit(marker);
        let _ = msg_cstr;
        res
    }
    /// Returns [`Exception`] that is instance of **`System.ArgumentNullException`**
    #[must_use]
    pub fn argument_null(msg: &str) -> Self {
        let msg_cstr = CString::new(msg).expect(crate::STR2CSTR_ERR);
        #[cfg(feature = "referneced_objects")]
        let marker = gc_unsafe_enter();
        let res = unsafe {
            Self::from_ptr_unchecked(
                crate::binds::mono_get_exception_argument_null(msg_cstr.as_ptr()).cast(),
            )
        };
        #[cfg(feature = "referneced_objects")]
        gc_unsafe_exit(marker);
        let _ = msg_cstr;
        res
    }
    /// Returns [`Exception`] that is instance of **`System.ArgumentOutOfRangeException`**
    #[must_use]
    pub fn argument_out_of_range(msg: &str) -> Self {
        let msg_cstr = CString::new(msg).expect(crate::STR2CSTR_ERR);
        #[cfg(feature = "referneced_objects")]
        let marker = gc_unsafe_enter();
        let res = unsafe {
            Self::from_ptr_unchecked(
                crate::binds::mono_get_exception_argument_out_of_range(msg_cstr.as_ptr()).cast(),
            )
        };
        #[cfg(feature = "referneced_objects")]
        gc_unsafe_exit(marker);
        let _ = msg_cstr;
        res
    }
    /// Returns [`Exception`] that is instance of **`System.ArithmeticException`**
    #[must_use]
    pub fn arithmetic() -> Self {
        #[cfg(feature = "referneced_objects")]
        let marker = gc_unsafe_enter();
        let res = unsafe {
            Self::from_ptr_unchecked(crate::binds::mono_get_exception_arithmetic().cast())
        };
        #[cfg(feature = "referneced_objects")]
        gc_unsafe_exit(marker);
        res
    }
    /// Returns [`Exception`] that is instance of **`System.ArrayTypeMismatchException`**
    #[must_use]
    pub fn array_type_mismatch() -> Self {
        #[cfg(feature = "referneced_objects")]
        let marker = gc_unsafe_enter();
        let res = unsafe {
            Self::from_ptr_unchecked(crate::binds::mono_get_exception_array_type_mismatch().cast())
        };
        #[cfg(feature = "referneced_objects")]
        gc_unsafe_exit(marker);
        res
    }
    /// Returns [`Exception`] that is instance of **`System.BadImageFormatException`**
    #[must_use]
    pub fn bad_image_format(msg: &str) -> Self {
        let msg_cstr = CString::new(msg).expect(crate::STR2CSTR_ERR);
        #[cfg(feature = "referneced_objects")]
        let marker = gc_unsafe_enter();
        let res = unsafe {
            Self::from_ptr_unchecked(
                crate::binds::mono_get_exception_bad_image_format(msg_cstr.as_ptr()).cast(),
            )
        };
        #[cfg(feature = "referneced_objects")]
        gc_unsafe_exit(marker);
        let _ = msg_cstr;
        res
    }
    /// Returns [`Exception`] that is instance of **`System.CannotUnloadAppdomain`**
    #[must_use]
    pub fn cannot_unload_appdomain(msg: &str) -> Self {
        let msg_cstr = CString::new(msg).expect(crate::STR2CSTR_ERR);
        #[cfg(feature = "referneced_objects")]
        let marker = gc_unsafe_enter();
        let res = unsafe {
            Self::from_ptr_unchecked(
                crate::binds::mono_get_exception_cannot_unload_appdomain(msg_cstr.as_ptr()).cast(),
            )
        };
        #[cfg(feature = "referneced_objects")]
        gc_unsafe_exit(marker);
        let _ = msg_cstr;
        res
    }
    /// Returns [`Exception`] that is instance of **`System.AppDomainUnloadedException`**
    #[must_use]
    pub fn domain_unloaded() -> Self {
        #[cfg(feature = "referneced_objects")]
        let marker = gc_unsafe_enter();
        let res = unsafe {
            Self::from_ptr_unchecked(crate::binds::mono_get_exception_appdomain_unloaded().cast())
        };
        #[cfg(feature = "referneced_objects")]
        gc_unsafe_exit(marker);
        res
    }
    /// Returns [`Exception`] that is instance of **`System.DivideByZeroException`**
    #[must_use]
    pub fn divide_by_zero() -> Self {
        #[cfg(feature = "referneced_objects")]
        let marker = gc_unsafe_enter();
        let res = unsafe {
            Self::from_ptr_unchecked(crate::binds::mono_get_exception_divide_by_zero().cast())
        };
        #[cfg(feature = "referneced_objects")]
        gc_unsafe_exit(marker);
        res
    }
    /// Returns [`Exception`] that is instance of **`System.ExecutionEngineException`**
    #[must_use]
    pub fn execution_engine_exception(msg: &str) -> Self {
        let msg_cstr = CString::new(msg).expect(crate::STR2CSTR_ERR);
        #[cfg(feature = "referneced_objects")]
        let marker = gc_unsafe_enter();
        let res = unsafe {
            Self::from_ptr_unchecked(
                crate::binds::mono_get_exception_execution_engine(msg_cstr.as_ptr()).cast(),
            )
        };
        #[cfg(feature = "referneced_objects")]
        gc_unsafe_exit(marker);
        let _ = msg_cstr;
        res
    }
    /// Returns [`Exception`] that is instance of **`System.IO.FileNotFoundException`**
    #[must_use]
    pub fn file_not_found(fname: &str) -> Self {
        let fname_cstr = CString::new(fname).expect(crate::STR2CSTR_ERR);
        let ms = unsafe { crate::binds::mono_string_new_wrapper(fname_cstr.as_ptr()) };
        let _ = fname_cstr;
        #[cfg(feature = "referneced_objects")]
        let marker = gc_unsafe_enter();
        let res = unsafe {
            Self::from_ptr_unchecked(crate::binds::mono_get_exception_file_not_found(ms).cast())
        };
        #[cfg(feature = "referneced_objects")]
        gc_unsafe_exit(marker);
        res
    }
    /// Returns [`Exception`] that is instance of **`System.IndexOutOfRangeException`**
    #[must_use]
    pub fn index_out_of_range() -> Self {
        #[cfg(feature = "referneced_objects")]
        let marker = gc_unsafe_enter();
        let res = unsafe {
            Self::from_ptr_unchecked(crate::binds::mono_get_exception_index_out_of_range().cast())
        };
        #[cfg(feature = "referneced_objects")]
        gc_unsafe_exit(marker);
        res
    }
    /// Returns [`Exception`] that is instance of **`System.InvalidCastException`**
    #[must_use]
    pub fn invald_cast() -> Self {
        #[cfg(feature = "referneced_objects")]
        let marker = gc_unsafe_enter();
        let res = unsafe {
            Self::from_ptr_unchecked(crate::binds::mono_get_exception_invalid_cast().cast())
        };
        #[cfg(feature = "referneced_objects")]
        gc_unsafe_exit(marker);
        res
    }
    /// Returns [`Exception`] that is instance of **`System.IO.IOException`**
    #[must_use]
    pub fn io_exception(msg: &str) -> Self {
        let msg_cstr = CString::new(msg).expect(crate::STR2CSTR_ERR);
        #[cfg(feature = "referneced_objects")]
        let marker = gc_unsafe_enter();
        let res = unsafe {
            Self::from_ptr_unchecked(crate::binds::mono_get_exception_io(msg_cstr.as_ptr()).cast())
        };
        #[cfg(feature = "referneced_objects")]
        gc_unsafe_exit(marker);
        let _ = msg_cstr;
        res
    }
    /// Returns [`Exception`] that is instance of **`System.MissingMethodException`**
    #[must_use]
    pub fn missing_method(class_name: &str, member_name: &str) -> Self {
        let class_name_cstr = CString::new(class_name).expect(crate::STR2CSTR_ERR);
        let member_name_cstr = CString::new(member_name).expect(crate::STR2CSTR_ERR);
        #[cfg(feature = "referneced_objects")]
        let marker = gc_unsafe_enter();
        let res = unsafe {
            Self::from_ptr_unchecked(
                crate::binds::mono_get_exception_missing_method(
                    class_name_cstr.as_ptr(),
                    member_name_cstr.as_ptr(),
                )
                .cast(),
            )
        };
        #[cfg(feature = "referneced_objects")]
        gc_unsafe_exit(marker);
        let _ = class_name_cstr;
        let _ = member_name_cstr;
        res
    }
    /// Returns [`Exception`] that is instance of **`System.NullReferenceException`**
    #[must_use]
    pub fn null_reference() -> Self {
        #[cfg(feature = "referneced_objects")]
        let marker = gc_unsafe_enter();
        let res = unsafe {
            Self::from_ptr_unchecked(crate::binds::mono_get_exception_null_reference().cast())
        };
        #[cfg(feature = "referneced_objects")]
        gc_unsafe_exit(marker);
        res
    }
    /// Returns [`Exception`] that is instance of **`System.OverflowException`**
    #[must_use]
    pub fn overflow() -> Self {
        #[cfg(feature = "referneced_objects")]
        let marker = gc_unsafe_enter();
        let res =
            unsafe { Self::from_ptr_unchecked(crate::binds::mono_get_exception_overflow().cast()) };
        #[cfg(feature = "referneced_objects")]
        gc_unsafe_exit(marker);
        res
    }
    /// Returns [`Exception`] that is instance of **`System.Security.SecurityException`**
    #[must_use]
    pub fn security() -> Self {
        #[cfg(feature = "referneced_objects")]
        let marker = gc_unsafe_enter();
        let res =
            unsafe { Self::from_ptr_unchecked(crate::binds::mono_get_exception_security().cast()) };
        #[cfg(feature = "referneced_objects")]
        gc_unsafe_exit(marker);
        res
    }
    /// Returns [`Exception`] that is instance of **`System.Runtime.Serialization.SerializationException`**
    #[must_use]
    pub fn serialization_exception(msg: &str) -> Self {
        let msg_cstr = CString::new(msg).expect(crate::STR2CSTR_ERR);
        #[cfg(feature = "referneced_objects")]
        let marker = gc_unsafe_enter();
        let res = unsafe {
            Self::from_ptr_unchecked(
                crate::binds::mono_get_exception_serialization(msg_cstr.as_ptr()).cast(),
            )
        };
        #[cfg(feature = "referneced_objects")]
        gc_unsafe_exit(marker);
        let _ = msg_cstr;
        res
    }
    /// Returns [`Exception`] that is instance of **`System.StackOverflowException`**
    #[must_use]
    pub fn stack_overflow() -> Self {
        #[cfg(feature = "referneced_objects")]
        let marker = gc_unsafe_enter();
        let res = unsafe {
            Self::from_ptr_unchecked(crate::binds::mono_get_exception_stack_overflow().cast())
        };
        #[cfg(feature = "referneced_objects")]
        gc_unsafe_exit(marker);
        res
    }
    /// Returns [`Exception`] that is instance of **`System.SynchronizationLockException`**
    #[must_use]
    pub fn synchronization_lock(msg: &str) -> Self {
        let msg_cstr = CString::new(msg).expect(crate::STR2CSTR_ERR);
        #[cfg(feature = "referneced_objects")]
        let marker = gc_unsafe_enter();
        let res = unsafe {
            Self::from_ptr_unchecked(
                crate::binds::mono_get_exception_synchronization_lock(msg_cstr.as_ptr()).cast(),
            )
        };
        #[cfg(feature = "referneced_objects")]
        gc_unsafe_exit(marker);
        let _ = msg_cstr;
        res
    }
    /// Returns [`Exception`] that is instance of **`System.Threading.ThreadAbortException`**
    #[must_use]
    pub fn thread_abort() -> Self {
        #[cfg(feature = "referneced_objects")]
        let marker = gc_unsafe_enter();
        let res = unsafe {
            Self::from_ptr_unchecked(crate::binds::mono_get_exception_thread_abort().cast())
        };
        #[cfg(feature = "referneced_objects")]
        gc_unsafe_exit(marker);
        res
    }
    /// Returns [`Exception`] that is instance of **`System.Threading.ThreadStateException`**
    #[must_use]
    pub fn thread_sate(msg: &str) -> Self {
        let msg_cstr = CString::new(msg).expect(crate::STR2CSTR_ERR);
        #[cfg(feature = "referneced_objects")]
        let marker = gc_unsafe_enter();
        let res = unsafe {
            Self::from_ptr_unchecked(
                crate::binds::mono_get_exception_thread_state(msg_cstr.as_ptr()).cast(),
            )
        };
        #[cfg(feature = "referneced_objects")]
        gc_unsafe_exit(marker);
        let _ = msg_cstr;
        res
    }
    /// Returns [`Exception`] that is instance of **`System.TypeInitializationException`** with *`type_name`* and inner exception *`inner`*.
    #[must_use]
    pub fn type_initialization(type_name: &str, inner: &Self) -> Self {
        let type_name_cstr = CString::new(type_name).expect(crate::STR2CSTR_ERR);
        #[cfg(feature = "referneced_objects")]
        let marker = gc_unsafe_enter();
        let res = unsafe {
            Self::from_ptr_unchecked(
                crate::binds::mono_get_exception_type_initialization(
                    type_name_cstr.as_ptr(),
                    inner.get_ptr().cast(),
                )
                .cast(),
            )
        };
        #[cfg(feature = "referneced_objects")]
        gc_unsafe_exit(marker);
        let _ = type_name_cstr;
        res
    }
    /// Returns [`Exception`] that is instance of **`System.TypeLoadException`**
    #[must_use]
    pub fn type_load(class_name: &str, member_name: &str) -> Self {
        let class_name_cstr = CString::new(class_name).expect(crate::STR2CSTR_ERR);
        #[cfg(feature = "referneced_objects")]
        let marker = gc_unsafe_enter();
        let cn_mono_string =
            unsafe { crate::binds::mono_string_new_wrapper(class_name_cstr.as_ptr()) };
        let _ = class_name_cstr;
        let member_name_cstr = CString::new(member_name).expect(crate::STR2CSTR_ERR);
        let res = unsafe {
            Self::from_ptr_unchecked(
                crate::binds::mono_get_exception_type_load(
                    cn_mono_string,
                    member_name_cstr.as_ptr() as *mut i8,
                )
                .cast(),
            )
        };
        #[cfg(feature = "referneced_objects")]
        gc_unsafe_exit(marker);
        let _ = member_name_cstr;
        res
    }
    /// Returns [`Exception`] that is instance of **`System.InvalidOperationException`**
    #[must_use]
    pub fn invalid_operation(msg: &str) -> Self {
        let msg_cstr = CString::new(msg).expect(crate::STR2CSTR_ERR);
        #[cfg(feature = "referneced_objects")]
        let marker = gc_unsafe_enter();
        let res = unsafe {
            Self::from_ptr_unchecked(
                crate::binds::mono_get_exception_invalid_operation(msg_cstr.as_ptr()).cast(),
            )
        };
        #[cfg(feature = "referneced_objects")]
        gc_unsafe_exit(marker);
        let _ = msg_cstr;
        res
    }
    /// Returns [`Exception`] that is instance of **`System.MissingFieldException`**
    #[must_use]
    pub fn missing_field(class_name: &str, member_name: &str) -> Self {
        let class_name_cstr = CString::new(class_name).expect(crate::STR2CSTR_ERR);
        let member_name_cstr = CString::new(member_name).expect(crate::STR2CSTR_ERR);
        #[cfg(feature = "referneced_objects")]
        let marker = gc_unsafe_enter();
        let res = unsafe {
            Self::from_ptr_unchecked(
                crate::binds::mono_get_exception_missing_field(
                    class_name_cstr.as_ptr(),
                    member_name_cstr.as_ptr() as *mut i8,
                )
                .cast(),
            )
        };
        #[cfg(feature = "referneced_objects")]
        gc_unsafe_exit(marker);
        let _ = member_name_cstr;
        let _ = class_name_cstr;
        res
    }
    /// Returns [`Exception`] that is instance of **`System.NotSupportedException`**
    #[must_use]
    pub fn not_supported(msg: &str) -> Self {
        let msg_cstr = CString::new(msg).expect(crate::STR2CSTR_ERR);
        #[cfg(feature = "referneced_objects")]
        let marker = gc_unsafe_enter();
        let res = unsafe {
            Self::from_ptr_unchecked(
                crate::binds::mono_get_exception_not_supported(msg_cstr.as_ptr()).cast(),
            )
        };
        #[cfg(feature = "referneced_objects")]
        gc_unsafe_exit(marker);
        let _ = msg_cstr;
        res
    }
    /// Returns [`Exception`] that is instance of **`System.FieldAccessException`**
    #[must_use]
    pub fn field_access() -> Self {
        #[cfg(feature = "referneced_objects")]
        let marker = gc_unsafe_enter();
        let res = unsafe {
            Self::from_ptr_unchecked(crate::binds::mono_get_exception_field_access().cast())
        };
        #[cfg(feature = "referneced_objects")]
        gc_unsafe_exit(marker);
        res
    }
    /// Returns [`Exception`] that is instance of **`System.MethodAccessException`**
    #[must_use]
    pub fn method_access() -> Self {
        #[cfg(feature = "referneced_objects")]
        let marker = gc_unsafe_enter();
        let res = unsafe {
            Self::from_ptr_unchecked(crate::binds::mono_get_exception_method_access().cast())
        };
        #[cfg(feature = "referneced_objects")]
        gc_unsafe_exit(marker);
        res
    }
    /// Returns [`Exception`] that is instance of **`System.OutOfMemoryException`**
    #[must_use]
    pub fn out_of_memory() -> Self {
        #[cfg(feature = "referneced_objects")]
        let marker = gc_unsafe_enter();
        let res = unsafe {
            Self::from_ptr_unchecked(crate::binds::mono_get_exception_out_of_memory().cast())
        };
        #[cfg(feature = "referneced_objects")]
        gc_unsafe_exit(marker);
        res
    }
    /// Creates [`Exception`] with a wraped inner [`Exception`] *inner*.
    #[must_use]
    pub fn wrapped(inner: &Self) -> Self {
        #[cfg(feature = "referneced_objects")]
        let marker = gc_unsafe_enter();
        let res = unsafe {
            Self::from_ptr_unchecked(
                crate::binds::mono_get_exception_runtime_wrapped(inner.get_ptr().cast()).cast(),
            )
        };
        #[cfg(feature = "referneced_objects")]
        gc_unsafe_exit(marker);
        res
    }
}
/// Variant of except which instead of panicking will raise a managed exception.
pub(crate) fn except_managed<T: Sized>(option: Option<T>, msg: &str) -> T {
    option.map_or_else(
        || {
            let exc = Exception::argument_null(&format!(
                "Value of type: \"{}\" was null!\"{}\"",
                std::any::type_name::<T>(),
                &msg
            ));
            unsafe {
                exc.raise();
            }
        },
        |t| t,
    )
}
/*
/// Variant of except which instead of panicking will raise a managed exception.
pub(crate) fn unwrap_managed<T: Sized>(option: Option<T>) -> T {
    if let Some(t) = option {
        t
    } else {
        let exc = Exception::argument_null(&format!(
            "Value of type: \"{}\" was null!",
            std::any::type_name::<T>()
        ));
        unsafe{exc.raise();}
    }
}*/
use core::fmt::Formatter;
impl core::fmt::Debug for Exception {
    fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), std::fmt::Error> {
        let mstr = self
            .to_mstring()
            .expect("Got an exception while trying to convert an exception to string!")
            .expect(
                "Got null instead of a string while trying to convert an exception to a string!",
            )
            .to_string();
        write!(f, "Exception:\"{mstr}\"")
    }
}
impl crate::object::ObjectTrait for Exception {
    #[must_use]
    unsafe fn from_ptr_unchecked(exc_ptr: *mut MonoObject) -> Self {
        #[cfg(not(feature = "referneced_objects"))]
        {
            Self {
                exc_ptr: exc_ptr.cast(),
            }
        }
        #[cfg(feature = "referneced_objects")]
        {
            Self {
                handle: GCHandle::create_default(exc_ptr.cast()),
            }
        }
    }
    #[must_use]
    fn get_ptr(&self) -> *mut MonoObject {
        #[cfg(not(feature = "referneced_objects"))]
        {
            self.exc_ptr.cast()
        }
        #[cfg(feature = "referneced_objects")]
        {
            self.handle.get_target().cast()
        }
    }
}
impl InteropClass for Exception {
    fn get_mono_class() -> Class {
        Class::get_exception_class()
    }
}
impl Clone for Exception {
    fn clone(&self) -> Self {
        unsafe { Self::from_ptr_unchecked(self.get_ptr().cast()) } //If exception exists then it can't be null
    }
}
impl<O: ObjectTrait> PartialEq<O> for Exception {
    fn eq(&self, other: &O) -> bool {
        self.get_ptr().cast() == other.get_ptr()
    }
}
impl std::fmt::Display for Exception {
    fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), std::fmt::Error> {
        let mstr = self.to_mstring();
        write!(
            f,
            "{}",
            mstr.expect("Got an exception while converting an exception String!")
                .expect("Got null from converting exception to string!")
                .to_string()
        )
    }
}